React-Native fetch 中的“then(res => res.json())”是什么意思? [复制]

2023-11-22

是什么意思then(res => res.json())在下面的react-native fetch 代码片段中?

fetch(url)
      .then(res => res.json())
      .then(res => {
        this.setState({
          data: res,
          error: res.error || null,
          loading: false
        });

这并不是一个真正的反应问题,因为 fetch 和 then 是 js 本身的一部分。

fetch 返回一个 Promise 对象,其中包含各种信息,如标头、HTTP 状态等。

你有res.json()以及其他各种可能性。.json()只会将主体作为带有 json 内容的承诺返回。

欲了解更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

您可以按如下方式返回数据:

  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React-Native fetch 中的“then(res => res.json())”是什么意思? [复制] 的相关文章

随机推荐