Fetch API 提供用于抓取资源 (包括整个网络) 的接口。大家似乎很熟悉对有使用过
，但新 API 提供更强大、更灵活的特征集。
Fetch provides a generic definition of
objects (and other things involved with network requests). This will allow them to be used wherever they are needed in the future, whether it’s for service workers, Cache API, and other similar things that handle or modify requests and responses, or any kind of use case that might require you to generate your responses programmatically (that is, the use of computer program or personal programming instructions).
It also defines related concepts such as CORS and the HTTP Origin header semantics, supplanting their separate definitions elsewhere.
For making a request and fetching a resource, use the
method. It is implemented in multiple interfaces, specifically
. This makes it available in pretty much any context you might want to fetch resources in.
method takes one mandatory argument, the path to the resource you want to fetch. It returns a
that resolves to the
to that request, whether it is successful or not. You can also optionally pass in an
options object as the second argument (see
You can create a request and response directly using the
constructors, but it's uncommon to do this directly. Instead, these are more likely to be created as results of other API actions (for example,
from service workers).
in three main ways:
fetch()won’t reject on HTTP error status even if the response is an HTTP
500. Instead, it will resolve normally (with
okstatus set to
false), and it will only reject on network failure or if anything prevented the request from completing.
Set-Cookieheaders from other sites are silently ignored.
fetch()won’t send cookies , unless you set
'same-origin'. The following browsers shipped and outdated native fetch, and were updated in these versions:
credentials: 'same-origin'init option on all api requests that may be affected by cookies/user login state.
Browsers have started to add experimental support for the
interfaces (aka The Abort API), which allow operations like Fetch and XHR to be aborted if they have not already completed. See the interface pages for more details.
fetch()method used to fetch a resource.
||Chrome 42||Edge 14||
||Safari 10.1||WebView Android 42||Chrome Android 42||
||Safari iOS 10.3||Samsung Internet Android 4.0|
|Support for blob: and data:||Chrome 48||Edge 79||Firefox ?||IE No||Opera ?||Safari ?||WebView Android 43||Chrome Android 48||Firefox Android ?||Opera Android ?||Safari iOS ?||Samsung Internet Android 5.0|
|referrerPolicy||Chrome 52||Edge 79||Firefox 52||IE No||Opera 39||Safari 11.1||WebView Android 52||Chrome Android 52||Firefox Android 52||Opera Android 41||Safari iOS No||Samsung Internet Android 6.0|
|signal||Chrome 66||Edge 16||Firefox 57||IE No||Opera 53||Safari 11.1||WebView Android 66||Chrome Android 66||Firefox Android 57||Opera Android 47||Safari iOS 11.3||Samsung Internet Android 9.0|
|Streaming response body||Chrome 43||Edge 14||
||IE No||Opera 29||Safari 10.1||WebView Android 43||Chrome Android 43||Firefox Android No||Opera Android No||Safari iOS 10.3||Samsung Internet Android 4.0|