“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

后端 未结 17 1402
孤城傲影
孤城傲影 2020-11-21 04:42

I\'m developing a page that pulls images from Flickr and Panoramio via jQuery\'s AJAX support.

The Flickr side is working fine, but when I try to $.get(url, ca

相关标签:
17条回答
  • 2020-11-21 05:37

    As long as the requested server supports the JSON data format, use the JSONP (JSON Padding) interface. It allows you to make external domain requests without proxy servers or fancy header stuff.

    0 讨论(0)
  • 2020-11-21 05:38

    For the record, as far as I can tell, you had two problems:

    1. You weren't passing a "jsonp" type specifier to your $.get, so it was using an ordinary XMLHttpRequest. However, your browser supported CORS (Cross-Origin Resource Sharing) to allow cross-domain XMLHttpRequest if the server OKed it. That's where the Access-Control-Allow-Origin header came in.

    2. I believe you mentioned 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: * (which, if you were reaching Flickr via $.get, they must have been doing) while the other was to echo back the contents of the Origin header. However, file:// URLs produce a null Origin which can't be authorized via echo-back.

    The first was solved in a roundabout way by Darin's suggestion to use $.getJSON. It does a little magic to change the request type from its default of "json" to "jsonp" if it sees the substring callback=? in the URL.

    That solved the second by no longer trying to perform a CORS request from a file:// URL.

    To clarify for other people, here are the simple troubleshooting instructions:

    1. If you're trying to use JSONP, make sure one of the following is the case:
      • You're using $.get and set dataType to jsonp.
      • You're using $.getJSON and included callback=? in the URL.
    2. If you're trying to do a cross-domain XMLHttpRequest via CORS...
      1. Make sure you're testing via http://. Scripts running via file:// have limited support for CORS.
      2. Make sure the browser actually supports CORS. (Opera and Internet Explorer are late to the party)
    0 讨论(0)
  • 2020-11-21 05:38

    It's the same origin policy, you have to use a JSON-P interface or a proxy running on the same host.

    0 讨论(0)
  • 2020-11-21 05:39

    Works for me on Google Chrome v5.0.375.127 (I get the alert):

    $.get('http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&callback=?&minx=-30&miny=0&maxx=0&maxy=150',
    function(json) {
        alert(json.photos[1].photoUrl);
    });
    

    Also I would recommend you using the $.getJSON() method instead as the previous doesn't work on IE8 (at least on my machine):

    $.getJSON('http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&callback=?&minx=-30&miny=0&maxx=0&maxy=150', 
    function(json) {
        alert(json.photos[1].photoUrl);
    });
    

    You may try it online from here.


    UPDATE:

    Now that you have shown your code I can see the problem with it. You are having both an anonymous function and inline function but both will be called processImages. That's how jQuery's JSONP support works. Notice how I am defining the callback=? so that you can use an anonymous function. You may read more about it in the documentation.

    Another remark is that you shouldn't call eval. The parameter passed to your anonymous function will already be parsed into JSON by jQuery.

    0 讨论(0)
  • 2020-11-21 05:39

    If you are doing local testing or calling the file from something like file:// then you need to disable browser security.

    On MAC: open -a Google\ Chrome --args --disable-web-security

    0 讨论(0)
提交回复
热议问题