[Solved-4 Solutions] XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
Error Description:
- If we develop a page that pulls images from Flickr and Panoramio via jQuery's AJAX support, flickr is working fine, but when we try to
$.get(url, callback)
from Panoramio, we see an error in Chrome's console:
XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin.
Solution 1:
- We have two problems:
- We weren't passing a "jsonp" type specifier to the
$.get
, so it was using an ordinary XMLHttpRequest. However, the browser supported CORS (Cross-Origin Resource Sharing) to allow cross-domain XMLHttpRequest if the server OKed it. That's where theAccess-Control-Allow-Origin
header came in. - You were running it from a file:// URL. There are two ways for CORS headers to signal that a cross-domain XHR is OK. One is to send
Access-Control-Allow-Origin: *
while the other was to echo back the contents of theOrigin
header. However,file://
URLs produce a nullOrigin
which can't be authorized via echo-back.
- The first can be solved by changing the request type from its default of "json" to "jsonp" if it sees the substring
callback=?
in the URL. - We can solve the second by no longer trying to perform a CORS request from a
file://
URL.
Solution 2:
- We may need to add a HEADER in your called script. In PHP:
Solution 3:
- For a simple HTML project:
- Then browse the file.
Solution 4:
- We can solve it via the
http.conf
file (edit and then restart the HTTP service):
- In the
Header set Access-Control-Allow-Origin "*"
, we can put a precise URL.