从文件:// URL运行的应用程序发出的请求出现“ Access-Control-Allow-Origin不允许使用Origin null”错误 [英] “Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

查看:538
本文介绍了从文件:// URL运行的应用程序发出的请求出现“ Access-Control-Allow-Origin不允许使用Origin null”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个页面,该页面通过jQuery的AJAX支持从Flickr和Panoramio中提取图像。

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

Flickr方面运行良好,但是当我尝试<$ Panoramio的c $ c> $。get(URL,回调),我在Chrome的控制台中看到错误:

The Flickr side is working fine, but when I try to $.get(url, callback) from Panoramio, I see an error in Chrome's console:


XMLHttpRequest无法加载 http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test& ; offset = 0& length = 20& callback = processImages& minx = -30&miny = 0& maxx = 0& maxy = 150

如果我直接从浏览器查询该URL,它就可以正常工作。这是怎么回事,我可以解决这个问题吗?我是在错误地编写查询,还是Panoramio这样做是为了阻碍我尝试做的事情?

If I query that URL from a browser directly it works fine. What is going on, and can I get around this? Am I composing my query incorrectly, or is this something that Panoramio does to hinder what I'm trying to do?

Google并未在查询结果中显示任何有用的匹配项错误消息

Google didn't turn up any useful matches on the error message.

EDIT

以下是一些示例代码显示问题:

Here's some sample code that shows the problem:

$().ready(function () {
  var url = 'http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&callback=processImages&minx=-30&miny=0&maxx=0&maxy=150';

  $.get(url, function (jsonp) {
    var processImages = function (data) {
      alert('ok');
    };

    eval(jsonp);
  });
});

您可以在线运行示例

编辑2

感谢达林在此方面的帮助。 上面的代码很错误。改用它:

Thanks to Darin for his help with this. THE ABOVE CODE IS WRONG. Use this instead:

$().ready(function () {
  var url = 'http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&minx=-30&miny=0&maxx=0&maxy=150&callback=?';

  $.get(url, function (data) {
    // can use 'data' in here...
  });
});


推荐答案

据我所知,您有两个问题:

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


  1. 您没有将 jsonp类型说明符传递给您的 $ .get ,因此它使用的是普通的XMLHttpRequest。但是,您的浏览器支持CORS(跨域资源共享),如果服务器确定,则允许跨域XMLHttpRequest。这就是 Access-Control-Allow-Origin 标头的来源。

  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.

我相信您提到了您从file:// URL运行它。 CORS标头有两种方式来表示跨域XHR正常。一种是发送 Access-Control-Allow-Origin:* (如果您通过 $。get ,他们一定在做),而另一个是回显 Origin 标头的内容。但是, file:// URL会生成一个空的 Origin ,该值无法通过回显进行授权。

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.

第一个问题是Darin建议使用 $。getJSON 。如果在URL中看到子字符串 callback =?,则将请求类型从默认的 json更改为 jsonp确实有点魔术。

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.

解决了第二个问题,不再尝试从 file:// URL执行CORS请求。

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. 如果您正在尝试要使用JSONP,请确保是以下情况之一:


    • 您正在使用 $。get 并将 dataType 设置为 jsonp

    • 您正在使用 $。getJSON 并包含 callback =?网址中。

  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.

  1. 确保您正在通过 http:// 进行测试。通过 file:// 运行的脚本对CORS的支持有限。

  2. 确保浏览器实际上支持CORS 。 (Opera和Internet Explorer迟到了)

  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)


这篇关于从文件:// URL运行的应用程序发出的请求出现“ Access-Control-Allow-Origin不允许使用Origin null”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆