【发布时间】:2021-11-17 08:58:07
【问题描述】:
我正在使用 https://add0n.com/access-control.html 来绕过我正在制作的扩展程序的 CORS 政策,但是,我收到了这个错误:
Access to XMLHttpRequest at 'https://myexampleapi.com/' from origin 'https://www.somedomain.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
这是我用来访问 API 的代码:
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", 'https://api.example.com/test/getstats?userId=76561399568194663', true);
xmlHttp.setRequestHeader("ApiKey", "13cf02fb-fake-442f-f7cc-9ee44g48748b");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
console.log(xmlHttp.responseText)
}
}
xmlHttp.send(null);
此错误仅在 Chrome 中发生,Firefox 没有问题。我已经找了8个小时了,我越来越绝望了。有解决这个问题的建议吗?
【问题讨论】:
-
OPTIONS 预检请求的响应状态如何?
-
表示HTTP响应状态为0
-
如果你不控制服务器,你需要设置一个服务器作为代理,否则你无法绕过cors。如果您确实控制了服务器,则需要正确设置您的 cors 标头。
-
@Syllith - 这就是问题所在,
0不在200和299之间 -
老实说,我认为编写良好的扩展没有可绕过的 CORS 问题
标签: javascript api cors