【问题标题】:Getting false response and empty data array in react-native post save data to server在 react-native post 中获取错误响应和空数据数组将数据保存到服务器
【发布时间】:2019-10-16 18:44:45
【问题描述】:

我正在尝试使用fetch 将一个简单的注册表单保存到服务器,但得到了false 的响应。

apiDataSave = () => {
         fetch('<myapi>', {  
            method: 'POST',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                name : "abc",
                no : "1234567890",
                password : "123",
                email : "abc@gmail.com"  
            })
          })
          .then(function(response) {
              return response.json();
            })
          .then(function(data) {
              alert(`response json is: ${JSON.stringify(data)}`)
          })

}

我收到如下警报:

响应json为:{"status":false,"data":[]}

预期结果警报是:

response json is: {"status":true,"data":"1"} // 输入数据库的记录数

【问题讨论】:

    标签: react-native post fetch-api


    【解决方案1】:

    问题在于 Content-Type 不是 'application/json' 而是以下:

    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    

    我在这里得到了答案: stackoverflow answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多