【问题标题】:Content-type : application/json is not working for fetch{method:post}内容类型:应用程序/json 不适用于 fetch{method:post}
【发布时间】:2022-10-15 18:10:12
【问题描述】:

我在请求标头中的内容类型是 text/plain 但我设置了 content-type : application/json 和 mode : cors 。完成所有这些之后,我得到了文本/纯内容类型

export const signin = async({email, password}) =>{
console.log(email);
console.log(password);
const _data = {
    "email" : email,
    "password" : password
}

try{
    const response = await fetch(apiUrl+"/api/user/signin",{
        method : 'POST',
        mode: 'cors',
        body: JSON.stringify(_data),
        header :{
            "Content-Type": "application/json",
            "Content-Length": JSON.stringify(_data).length
        },
    });
    if(!response || !response.ok){
        // throw new Error(response.json());
        return await response.json();
    }
    return await response.json();
    
}catch(err){
    console.log(err);
    return {error : err.message};
}
    
};

以上是我的代码。请帮我

【问题讨论】:

  • 由于这个 json 数据没有到达后端(也许)
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: node.js mongodb post backend fetch-api


【解决方案1】:

尝试在 fetch 中使用选项对象上的 headers 属性而不是 header

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2021-06-21
    • 2011-09-13
    • 2017-11-17
    • 2013-11-17
    • 2015-04-21
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多