NextJS 中的 fetch 请求期间 Content-Type 不会改变

问题描述 投票:0回答:1

我尝试向远程 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
一起设置,但在请求时标题仍然没有改变

next.js tsx
1个回答
0
投票

从您的请求中删除

mode: "no-cors"
。简而言之,no-cors 不允许 json 内容类型。

类似问题的答案将告诉您原因。

© www.soinside.com 2019 - 2024. All rights reserved.