Fetchwindow.fetch 的 JavaScript polyfill

联合创作 · 2023-09-22 06:48

Fetch 是 window.fetch 的 JavaScript polyfill。

全局 fetch 函数是 web 请求和处理响应的简单方式,不使用 XMLHttpRequest。这个 polyfill 编写的接近标准的 Fetch 规范(https://fetch.spec.whatwg.org)。

fetch 函数支持所有的 HTTP 方式:

HTML:

fetch('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

JSON:

fetch('/users.json')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })
浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报