2017-08-12 96 views
0

我正在對服務器執行遠程獲取請求。有效負載採用JSON格式,因此我想將Content-Type標題更改爲application/json。我用下面的代碼來做到這一點:設置獲取請求中的內容類型不起作用

let email = document.getElementById("email").value 
let password = document.getElementById("password").value 

const body = {"email":email, "password":password} 
const headers = new Headers({ 
    "Content-Type": "application/json", 
    "Content-Length": JSON.stringify(body).length 
}) 
const options = { 
    method: "POST", 
    mode: "no-cors", 
    headers: headers, 
    body: JSON.stringify(body) 
} 
console.log(options) 
const response = await fetch('http://xx.xx.xx.xxx/login', options) 
const json = await response.json() 
console.log(json) 

然而,在Chrome開發人員工具控制檯,請求的Content-Type頭仍然是text/plain;charset=UTF-8。我究竟做錯了什麼?

+0

[Fetch:post json data,application/json change to text/plain]可能的重複(https://stackoverflow.com/questions/39689386/fetch-post-json-data-application-json-change-to -text-plain) – CodeZombie

回答

1

對於no-cors請求,不允許覆蓋Content-Type請求標頭。

將模式更改爲cors

+0

你是對的。 – guest271314