如何在带有标题json的React Native中使用fetch()?

时间:2019-02-08 13:10:13

标签: react-native fetch

邮递员输出:

Math.abs()

我在邮递员中得到这样的json。 当我尝试使用HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Fri, 08 Feb 2019 12:13:36 GMT {"status":1,"msg":"success","celeb":[{"id":1,"name":"Test Name".... 时,出现错误fetch()

json parse error, unknown identifier HTTP

2 个答案:

答案 0 :(得分:0)

let func = async () => {
 const url = 'https://myurl/fetch';
 const data = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          token: await AsyncStorage.getItem(ACCESS_TOKEN), /*or whatever you have on your api*/
        }),
      };

  const response = await fetch(url , data);
  const responseData = await response.json();

  console.log(responseData);
}

答案 1 :(得分:0)

您收到的答复(从邮递员打印的答复)已经是一个对象,您不必使用response.json()对其进行解析。删除它,它应该可以工作