【问题标题】:Getting error sending Post data to server将 Post 数据发送到服务器时出错
【发布时间】:2018-05-07 04:39:51
【问题描述】:

我通过 api 将数据发送到服务器。但我在响应时收到错误消息。

下面是我的代码

 fetch('http://35.196.195.208/SaveRecommendAppDetails', {
        method: 'POST',
        headers: {              
                 'Content-Type': 'application/json'
                 },
        body: JSON.stringify({
                  UserId:"23",
                  ClientId:"2",
                  Name: "Paras", 
                  Email: "Paras@gmail.com", 
                  Phone: "9876543210",
                  CreatedUserId:"23" ,
                  Company: "Brill Infosystem"})
      })
      .then((response) =>JSON.stringify(response.json())) 
      .then((responseJson) =>{console.log( "==========response=========" + responseJson) })    
      .catch((err) => { console.log("==========error=========" + err); });
    }

错误是: { "_40":0,"_65":0,"_55":null,"_72":null }

【问题讨论】:

    标签: reactjs react-native fetch fetch-api


    【解决方案1】:

    { "_40":0,"_65":0,"_55":null,"_72":null } 是当你 console.log 一个 Promise 对象时得到的。 response.json() 返回一个Promise,所以你不能只使用JSON.stringify() 它。移除对JSON.stringify()的调用。

    【讨论】:

    • 是的,他是对的,当您以字符串格式传递数字时,他会确保您的数据库/服务器中的字段是字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2018-04-17
    • 2012-12-29
    • 2021-06-05
    • 2011-09-12
    相关资源
    最近更新 更多