iT邦幫忙

0

ajax 的 content-type 在 safari/firefox 無法運行?

遇到一個詭異問題

$.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....

froce iT邦大師 1 級 ‧ 2019-04-10 12:55:26 檢舉
去比對chrome和safari 送出的檔頭吧
owo iT邦新手 5 級 ‧ 2019-04-10 15:24:16 檢舉
跨域請求要在 server 端允許才可以?
ccutmis iT邦高手 2 級 ‧ 2019-04-10 17:13:08 檢舉
https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Server-Side_Access_Control
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
淺水員
iT邦大師 6 級 ‧ 2019-04-10 16:17:15
最佳解答

參考: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 可以的原因我在猜是不是有裝一些允許跨域的外掛程式?

小魚 iT邦大師 1 級 ‧ 2019-04-11 09:24:59 檢舉

Chrome理論上也不能跨域,
外掛我就不清楚了。

我要發表回答

立即登入回答