遇到一個詭異問題
$.ajax({
type: 'POST',
url: 'https://xxx',
data: JSON.stringify({
"session": _session
}),
contentType: 'application/json',
dataType: 'json',
processData: false,
success: function () {
console.log('success');
}
});
這個在 mac/chrome 可以運行
但是在 mac/safari/firefox 上會出現錯誤:
Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
[Error] XMLHttpRequest cannot load https://xxx due to access control checks.
Failed to load resource: Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
這是咋了?
jquery 不支持 safari/firefox 嗎 lol....
參考:Access-Control-Allow-Headers
伺服器端要送 Access-Control-Allow-Headers 相關資訊
以 PHP 為例
<?php
//這邊為方便,Origin 設為 *(實際依照自己需求指定)
header('Access-Control-Allow-Origin: *');
//去掉下面這行的話就會發生錯誤:Request header field Content-Type ...(略)
header('Access-Control-Allow-Headers: Content-Type');
echo 'ok';
至於 chrome 可以的原因我在猜是不是有裝一些允許跨域的外掛程式?