首页 > 解决方案 > React native fetch 返回 _bodyBlob _bodyInit 标头等,而不是带有 json() 的 json

问题描述

在重新安装 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。不是我...

标签: jsonapireact-nativefetchblob

解决方案


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

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,但我很高兴。谢谢你。或者谢谢我,我不知道你告诉我。


推荐阅读