为什么我的 http://localhost CORS 来源不起作用? [英] Why does my http://localhost CORS origin not work?
问题描述
即使我使用适当的标头设置了服务器 (nginx/node.js),我也遇到了这个 CORS 问题.
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 无法加载 http://stackoverflow.com/.Access-Control-Allow-Origin 不允许 Origin http://localhost.
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 年出现的错误,2014 年标记为 WontFix).
Chrome does not support localhost for CORS requests (a bug opened in 2010, marked WontFix in 2014).
要解决这个问题,您可以使用像 localho.st
这样的域(它指向 127.0.0.1,就像 localhost 一样)或使用 --disable-web-security<启动 chrome/code> 标志(假设您只是在测试).
To get around this you can use a domain like localho.st
(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屋!