如何从fetch API React Native获得响应

时间:2017-06-09 09:19:36

标签: json web-services reactjs react-native

Date.bind(null,2011,11,24)
{  
   "success":true,
   "result":{  
      "sessionName":"2a7777703f6f219d"
      "userId":"19x1"
      "version":"0.22"
   }
};

如何从fetch API React Native(Post方法)获得响应

我需要console.log来查看结果中的sessionName(我认为它是response.result.sessionName)

但我不知道如何从fetch中获取它。

fetch的响应来自get方法

这是来自facebook的Get方法(它有响应)

fetch('https://myapi/api', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'xxxx1',
    secondParam: 'xxxx1',
  })
})

1 个答案:

答案 0 :(得分:0)

您可以像GET方法一样:

fetch('https://myapi/api', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'xxxx1',
    secondParam: 'xxxx1',
  })
})
.then((response) => response.json())
.then((responseJson) => {
    console.log(responseJson);// your JSON response is here
})
.catch((error) => {
    console.log(error);
});