简体   繁体   中英

How to get JSON data from an HTTPS url using JS?

I want to get data from a JSON file for my webpage, I searched a lot and I failed to find a request that succeeds with HTTPS.

I get this error when I run the HTML file on my machine using jQuery.getJSON() :

Origin null is not allowed by Access-Control-Allow-Origin.

Any ideas?

Add this header into an Apache .conf or .htaccess file

Header set Access-Control-Allow-Origin "*"

You may also need to add these:

Header set Access-Control-Allow-Methods POST, GET, OPTIONS
Header set Access-Control-Allow-Headers "X-SOME-HEADER"  (additional headers you need to pass)
Header always set Access-Control-Max-Age "(number of seconds to cache results)"

Before you launch for production, you should consider restricting the origin if possible. For example, Access-Control-Allow-Origin: example.com. If the Ajax requests will come from the same server, you should remove these headers entirely when you launch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM