fetch入门

2018-12-172089

语法

fetch函数

fetch(url, options).then(function(response) {
  // handle HTTP response
 }, function(error) {
  // handle network error
})

Request对象

  • 构造函数

    new Request(url, options)

Response对象

  • 属性

    • status

      HTTP response code in the 100–599 range

    • statusText

      Status text as reported by the server, e.g. "Unauthorized"

    • ok

      True if status is HTTP 2xx

    • headers

    • url

  • 响应体方法

    • text()
      yields the response text as String
    • json()
      yields the result of JSON.parse(responseText)
    • blob()
      yields a Blob
    • arrayBuffer()
      yields an ArrayBuffer
    • formData()
      yields FormData that can be forwarded to another request
  • 其他方法

    • clone()
    • error()
    • redirect()

Headers对象

  • 构造函数

    new Headers(hash)

  • 方法

    • has(name)
    • get(name)
    • set(name, value)
    • append(name, value)
    • delete(name)
    • forEach(function(value, name){ ... }, [thisContext])

Error对象

options参数说明

  • method

    HTTP request method. Default: "GET"

  • body

    HTTP request body, body types如下,

Class Default Content-Type
String text/plain;charset=UTF-8
URLSearchParams application/x-www-form- urlencoded;charset=UTF-8
FormData multipart/form-data
Blob inherited from the blob.type property
ArrayBuffer
TypedArray
DataView
  • headers

    Default: {}

  • credentials

    Authentication credentials mode. Default: "omit"

    • "omit"

      don't include authentication credentials (e.g. cookies) in the request

    • "same-origin"

      include credentials in requests to the same site

    • "include"

      include credentials in requests to all sites

** 作者简介:刘晓栋,芦苇科技web前端开发工程师。擅长各类終端官网建设、平台开发、公众号、微信小程序、小游戏,访问 www.talkmoney.cn 了解更多。**

分享
点赞1
打赏
上一篇:代理工具Fiddler -调试与替换接口状态
下一篇:微信小游戏上线准备