【问题标题】:React native fetch returns _bodyBlob _bodyInit headers etc instead of json with json()React native fetch 返回 _bodyBlob _bodyInit 标头等,而不是带有 json() 的 json
【发布时间】:2020-01-08 14:32:08
【问题描述】:

重新安装 node_modules 并将 json() 用于我的 fetch 调用后,我仍然收到 blob 响应。这是我的代码:

export function test() {
  const url = 'http://url/api/testServlet';
  return fetch(url, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json' 
    }
  })
  .then((data) => {
        console.log("data ", data, " --- end data");
        data.json();
  })
  .catch((error) => {
    console.error(error);
  });
}

使用 console.log,我得到:

data 
{
  "_bodyBlob": {
    blob
  }, 
  "_bodyInit": {
    blob
  }, 
  "headers": {
    headers
  }, 
  "ok": true, 
  "status": 200, 
  "statusText": undefined, 
  "type": "default", 
  "url": "http://url/api/testServlet"
}  
--- fin data

非常感谢您的帮助

编辑: 我还有一个错误 value.hasOwnProperty('tag') 来自任何地方我完全不知道它来自哪里......我只有一个显示我的 API 调用结果和上面显示的代码的组件。 消息是: "错误:value.hasOwnProperty 不是函数。(在'value.hasOwnProperty('tag')中,'value.hasOwnProperty'未定义)

我阅读了一些主题,所有人都在谈论这个错误,但是代码中写了函数 hasOwnProperty。不是我……

【问题讨论】:

    标签: json api react-native fetch blob


    【解决方案1】:

    我的错。或不。我不知道你告诉我。我替换为:

    export function test() {
      return fetch('http://url/api/testServlet')
        .then((response) => response.json())
        .then((responseJson) => {
          console.log("data : ", responseJson)
          return responseJson;
        })
        .catch((error) => {
          console.error(error);
        });
    }
    

    它奏效了。它可能来自我不知道的 const url,但我很高兴。谢谢你。或者谢谢我,我不知道你告诉我。

    【讨论】:

    • 我会告诉你:谢谢! :) 这篇文章对我有帮助,我删除了链接 then+logging 的错误用法。
    猜你喜欢
    • 2019-07-17
    • 2016-08-17
    • 2018-03-01
    • 2018-10-22
    • 2021-11-04
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多