Mod note: 这个问题是问为什么XMLHttpRequest
/fetch
/ETC。浏览器上的受相同访问策略限制(您会收到提及 CORB 或 CORS 的错误),而 Postman 则不受此限制。这个问题是not关于如何修复“No 'Access-Control-Allow-Origin'...”错误。这是关于它们发生的原因。
请停止发帖:
- 适用于所有语言/框架的 CORS 配置。反而.
- 允许请求规避 CORS 的第三方服务
- 用于关闭各种浏览器的 CORS 的命令行选项
我正在尝试使用授权JavaScript http://en.wikipedia.org/wiki/JavaScript通过连接到RESTful http://en.wikipedia.org/wiki/Representational_state_transfer#RESTful_web_services API http://en.wikipedia.org/wiki/Application_programming_interface内置Flask https://en.wikipedia.org/wiki/Flask_%28web_framework%29。但是,当我提出请求时,出现以下错误:
XMLHttpRequest cannot load http://myApiUrl/login.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.
我知道API或远程资源必须设置标头,但为什么当我通过Chrome扩展程序发出请求时它会起作用Postman https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?
这是请求代码:
$.ajax({
type: 'POST',
dataType: 'text',
url: api,
username: 'user',
password: 'pass',
crossDomain: true,
xhrFields: {
withCredentials: true,
},
})
.done(function (data) {
console.log('done');
})
.fail(function (xhr, textStatus, errorThrown) {
alert(xhr.responseText);
alert(textStatus);
});