获取API POST请求响应

时间:2017-10-24 07:53:58

标签: javascript react-native fetch

我试图在提交表单后从post请求中获取输出但是我在发布表单时得到了承诺响应而不是实际数据

fetch('localhost/clients', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(formData)
}).then(response => {
  console.info('Sending Message ...')
  console.info(response.json())
}).catch (error => {
  console.log(error)
})

数据传递到后端但是我想返回API服务器输出的数据。

1 个答案:

答案 0 :(得分:1)

response.json()返回一个Promise。您需要像下面一样使用它。

fetch('localhost/clients', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(formData)
}).then(response => {
  return response.json();
}).then(jsonResponse => {
  console.log(jsonResponse);
}).catch (error => {
  console.log(error)
})
相关问题