我尝试向远程 Django API 服务器发送 POST 请求,但收到错误,因为请求标头中的 Content-Type 没有更改,即使我明确指定了它。
请求标头:
Content-Length: 23
Content-Type: text/plain;charset=UTF-8
我的代码:
async function onSubmit(data: z.infer<typeof FormSchema>) {
try {
const response = await fetch(apiEndpoint, {
method: "POST",
mode: "no-cors",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (response.ok) {
console.log('Ok');
}
} catch (error) {
console.error(error);
}
}
我尝试仅指定
Content-Type
,也是根据类似问题的建议,我将Content-Type
与Accept
一起设置,但在请求时标题仍然没有改变