【问题标题】:react-native fetch returns odd responsereact-native fetch 返回奇怪的响应
【发布时间】:2016-11-08 14:18:33
【问题描述】:

我正在使用 fetch 从这样的 API 中获取一些东西:

fetch('http://facebook.github.io/react-native/movies.json')
        .then(
            data => console.log(data),
            error => console.log(error)
        )

但我得到的是以下对象,而不是实际数据

_bodyBlob: Blob
_bodyInit: Blob
headers: Headers
ok: true
status: 200
statusText: undefined
type: "default"
url: "http://facebook.github.io/react-native/movies.json"

谁能解释我怎么了? 我做错了什么?

【问题讨论】:

    标签: javascript react-native fetch


    【解决方案1】:

    要从响应中获取数据,您必须调用 data.json(); 示例:

    fetch('http://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
      })
      .catch((error) => {
        console.log(error);
      });
    

    (假设响应是 json 格式)。

    【讨论】:

    • 这不起作用。我几乎渴望看到来自发布请求的正文响应。它适用于邮递员,但不适用于本机应用程序
    • 我得到“在 'FileReader' 上执行 'readAsText' 失败:参数 1 不是 'Blob' 类型。”
    • 闻起来像是 RN 使用的另一个版本的 fetch。此响应(或其原型链)上没有没有jsontext 方法。这可能与我们正在使用的本地桥梁有关。来自 RN 的其他电话有 jsontext 符合预期。
    猜你喜欢
    • 2018-03-14
    • 2020-08-10
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2019-07-03
    • 2018-03-01
    相关资源
    最近更新 更多