javascript XMLHttpRequest 无法加载 URL。Access-Control-Allow-Origin 不允许的来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18733104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:00:17  来源:igfitidea点击:

XMLHttpRequest cannot load URL. Origin not allowed by Access-Control-Allow-Origin

javascriptxmlhttprequestcors

提问by Scott Newson

I want to make a small website that uses xml data from another domain. (Weather data from Weather Underground: www.wunderground.com). I am using just html and javascript, and writing it all in Visual Studio Express 2012 for Web.

我想制作一个使用来自另一个域的 xml 数据的小型网站。(来自 Weather Underground 的天气数据:www.wunderground.com)。我只使用 html 和 javascript,并在 Visual Studio Express 2012 for Web 中全部编写。

I make and send the xml request as follows:

我按如下方式发出并发送 xml 请求:

url = "http://api.wunderground.com/api/3c6e3d838e217361/geolookup/conditions/forecast/q/51.11999893,-114.01999664.xml";

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

The problem is that I get the following error in the Google Chrome (version 29.0.1547.66) developer console:

问题是我在 Google Chrome(版本 29.0.1547.66)开发人员控制台中收到以下错误:

XMLHttpRequest cannot load http://api.wunderground.com/api/3c6e3d838e217361/geolookup/conditions/forecast/q/51.11999893,-114.01999664.xml. Origin http://localhost:49933 is not allowed by Access-Control-Allow-Origin. 

Or this on Internet Explorer (version 10.0.8) console:

或者在 Internet Explorer(版本 10.0.8)控制台上:

SEC7118: XMLHttpRequest for http://api.wunderground.com/api/3c6e3d838e217361/geolookup/conditions/forecast/q/51.11999893,-114.01999664.xml required Cross Origin Resource Sharing (CORS). 

As I understand it, CORS (http://enable-cors.org/) needs effort by both the client and the server to work. I want to assume that the Weather Underground API knows what it is doing and has enabled things appropriately, such as setting the response header to include 'Access-Control-Allow-Origin: *', and I know that I get the same problems when I try the same code using another API provider (World Weather Online). So I think this is something I should be able to fix in my client code. Another SO answer where the suggestion is to fix the server-side header: CORS with XMLHttpRequest

据我了解,CORS(http://enable-cors.org/)需要客户端和服务器共同努力才能工作。我想假设 Weather Underground API 知道它在做什么并适当地启用了一些东西,例如将响应头设置为包含“Access-Control-Allow-Origin: *”,并且我知道当我遇到相同的问题时我使用另一个 API 提供程序(World Weather Online)尝试相同的代码。所以我认为这是我应该能够在我的客户端代码中修复的东西。另一个建议是修复服务器端标头的 SO 答案: CORS with XMLHttpRequest

I have tried to find answers, but don't understand articles such as: http://dev.opera.com/articles/view/dom-access-control-using-cross-origin-resource-sharing/http://saltybeagle.com/2009/09/cross-origin-resource-sharing-demo/

我试图找到答案,但不明白诸如: http: //dev.opera.com/articles/view/dom-access-control-using-cross-origin-resource-sharing/ http:// saltybeagle.com/2009/09/cross-origin-resource-sharing-demo/