fetch api的简单使用

2022-04-09  本文已影响0人  苍老师的眼泪

fetch会返回一个promise,仅当网络故障时或请求被阻止时,该promise才会标记为 reject,
当该promise 进入 resolve 状态是,我们resolve的参数名设置为response,这个response有 json 和 text 方法,
分别对应处理json 和普通字符串,这两个方法都返回一个将response处理成json或字符串的 promise,当该promise
resolve之后,才得到最终的结果result.

      fetch('http://localhost/api/index1').then(response => {
        return response.json()
      }).then(result => {
        console.log(result)
      })
上一篇 下一篇

猜你喜欢

热点阅读