問題描述
我正在嘗試使用 post 請求將文件發(fā)送到我的服務器,但是當它發(fā)送時會導致錯誤:
I'm trying to send files to my server with a post request, but when it sends it causes the error:
Access-Control-Allow-Headers 不允許請求標頭字段 Content-Type.
Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
所以我用谷歌搜索了錯誤并添加了標題:
So I googled the error and added the 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"
}
然后我得到錯誤:
Access-Control-Allow-Headers 不允許請求標頭字段 Access-Control-Allow-Origin
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers
所以我用谷歌搜索了這個問題,我能找到的唯一類似問題是提供了一半的答案,然后作為離題關閉.我應該添加/刪除哪些標題?
So I googled that and the only similar question I could find was provided a half answer then closed as off topic. What headers am I supposed to add/remove?
推薦答案
服務器(POST請求發(fā)送到的)需要包含Access-Control-Allow-Headers
標頭(等??)在其響應中.將它們放入來自客戶端的請求中沒有任何效果.
The server (that the POST request is sent to) needs to include the Access-Control-Allow-Headers
header (etc) in its response. Putting them in your request from the client has no effect.
這是因為由服務器指定它接受跨域請求(并且它允許 Content-Type
請求標頭,等等)——客戶端無法決定給定服務器本身應該允許 CORS.
This is because it is up to the server to specify that it accepts cross-origin requests (and that it permits the Content-Type
request header, and so on) – the client cannot decide for itself that a given server should allow CORS.
這篇關于Access-Control-Allow-Headers 不允許請求標頭字段 Access-Control-Allow-Headers的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!