我想爲Ajax CORS請求添加自定義標頭。如何爲Ajax CORS請求添加自定義標頭
我的服務器是PHP流明和代理服務器是nginx的,路徑 bootstrap/app.php
是我的項目配置
我想知道是否需要對服務器進行一些配置或在我的腳本中進行定義。這裏是我的代碼:
$.ajax({
url: "http://xx.api:8000/user/test",
dataType: 'json',
type: 'GET',
headers: {
"mark": "111",
},
success: function() {
console.info('sucess');
},
error: function() {
console.info("error");
}
});
當我建立在Chrome中,控制檯日誌是:
OPTIONS http://xx.api:8000/user/test 405 (Method Not Allowed)
XMLHttpRequest cannot load http://xx.api:8000/user/test. Invalid HTTP status code 405
請求頭是
OPTIONS /user/test HTTP/1.1
Host: xx.api:8000
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: null
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Access-Control-Request-Headers: accept, mark
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
的響應頭是
HTTP/1.1 405 Method Not Allowed
Server: nginx
Content-Type: application/json;charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.14
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
allow: GET
Cache-Control: no-cache, private
date: Tue, 22 Dec 2015 02:16:16 GMT
有什麼建議嗎?
什麼是您的網絡服務器? –
「xhr.setRequestHeader(」mark「,」authorizationToken「); //沒有工作」 - 「沒有工作」是什麼意思?請求是否已經完成?你有迴應嗎?控制檯是否顯示錯誤消息? **使用您的瀏覽器的開發工具** – Quentin
@RajeshManilal網絡服務器是PHP流明和代理服務器是Nginx,我編輯了我的問題。謝謝! – HuangMin