当http:// localhost是起源时致命的CORS [英] Deadly CORS when http://localhost is the origin
问题描述
我遇到了这个CORS问题,即使我使用适当的标头设置服务器(nginx / node.js)。
I am stuck with this CORS problem, even though I set the server (nginx/node.js) with the appropriate headers.
我可以在Chrome网络中看到窗格 - >响应标题:
I can see in Chrome Network pane -> Response Headers:
Access-Control-Allow-Origin:http://localhost
这应该可以解决问题。
这是我现在使用的代码测试:
Here's the code that I now use to test:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
console.log('xhr loaded');
};
xhr.open('GET', 'http://stackoverflow.com/');
xhr.send();
我得到
XMLHttpRequest cannot load http://stackoverflow.com/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
我怀疑这是客户端脚本中的问题,而不是服务器配置...
I suspect it's a problem in the client script and not server configuration...
推荐答案
Chrome 确实不支持localhost用于CORS请求(自2010年以来的一个漏洞)。
Chrome does not support localhost for CORS requests (an open bug since 2010).
要解决此问题,您可以使用 lvh这样的域名.me
(与localhost一样指向127.0.0.1)或使用 - disable-web-security
标志启动chrome(假设你'重新测试)。
To get around this you can use a domain like lvh.me
(which points at 127.0.0.1 just like localhost) or start chrome with the --disable-web-security
flag (assuming you're just testing).
这篇关于当http:// localhost是起源时致命的CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!