鍍金池/ 問答/HTML/ Axios跨域POST請求參數(shù)為什么不能使Object

Axios跨域POST請求參數(shù)為什么不能使Object

服務端PHP已經(jīng)開啟了跨域

header('Access-Control-Allow-Origin:*');

前端用Axios發(fā)送跨域請求

instance.post("/reward", {...reward}).then((response) => {
      let serverData = response.data
      if (serverData.flag) {
        cb(serverData.ret)
      } else {
        ecb(serverData.error)
      }
    }).catch((error) => {
      console.log(error)
      ecb(error)
    })

所有的其他設置均使用默認

問題:這個請求會觸發(fā)預檢測發(fā)送一個OPTIONS請求,恰巧服務端Nginx貌似不支持OPTIONS請求。
但是如果參數(shù)是JSON.stringify轉(zhuǎn)化后的字符串,就能夠正??缬?。
(手動設置過header的content-type,均無效)

回答
編輯回答
茍活

恰恰相反,nginx是最容易支持OPTIONS請求的服務器,只需要在配置里添加以下幾行就可以了:

location / {
    if ($request_method = OPTIONS ) {
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
}
2017年3月9日 23:11