繁体   English   中英

我如何从获取API React Native获取响应

[英]How can i get response from fetch API React Native

{  
   "success":true,
   "result":{  
      "sessionName":"2a7777703f6f219d"
      "userId":"19x1"
      "version":"0.22"
   }
};

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

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

我需要console.log查看结果中的sessionName(我认为是re​​sponse.result.sessionName)

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

像get方法一样从fetch获得的响应在哪里


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

 function getMoviesFromApiAsync() {
    return fetch('https://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        return responseJson.movies;
      })
      .catch((error) => {
        console.error(error);
      });
  }

您可以执行与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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM