我正在尝试使用 post 请求将文件发送到我的服务器,但是当它发送时会导致错误:
Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
所以我用谷歌搜索了错误并添加了标题:
$http.post($rootScope.URL, {params: arguments}, {headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "GET,POST,PUT,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
}
然后我得到错误:
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers
所以我用谷歌搜索了这个问题,我能找到的唯一类似问题是提供了一半的答案,然后作为离题关闭。我应该添加/删除哪些标题?
最佳答案
我遇到了同样的问题。 In the jQuery documentation我发现:
For cross-domain requests, setting the content type to anything other than
application/x-www-form-urlencoded
,multipart/form-data
, ortext/plain
will trigger the browser to send a preflight OPTIONS request to the server.
所以虽然服务器允许跨源请求但不允许 Access-Control-Allow-Headers
,它会抛出错误。默认情况下, Angular 内容类型是 application/json
,它正在尝试发送 OPTION 请求。尝试覆盖 Angular 默认 header 或在服务器端允许 Access-Control-Allow-Headers
。这是一个 Angular 示例:
$http.post(url, data, {
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
});
关于javascript - Access-Control-Allow-Headers 不允许请求 header 字段 Access-Control-Allow-Headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727306/