简体   繁体   English

在获取请求中设置Content-Type不起作用

[英]Setting Content-Type in fetch request not working

I am doing a remote fetch request to a server. 我正在对服务器执行远程获取请求。 The payload is in JSON format, so I want to change the Content-Type header to application/json . 有效负载为JSON格式,因此我想将Content-Type标头更改为application/json I have used the following code to do this: 我已使用以下代码执行此操作:

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)

However, in the Chrome developer tools console, the Content-Type header of the request is still text/plain;charset=UTF-8 . 但是,在Chrome开发人员工具控制台中,请求的Content-Type标头仍为text/plain;charset=UTF-8 What am I doing wrong? 我究竟做错了什么?

Overriding the Content-Type request header is not allowed for no-cors requests. no-cors请求不允许覆盖Content-Type请求标头。

Change the mode to cors . 将模式更改为cors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM