The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest
, but the new API provides a more powerful and flexible feature set.
Fetch provides a generic definition of Request
and Response
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 own responses programmatically.
It also provides a definition for 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 WindowOrWorkerGlobalScope.fetch()
method. It is implemented in multiple interfaces, specifically Window
and WorkerGlobalScope
. This makes it available in pretty much any context you might want to fetch resources in.
The fetch()
method takes one mandatory argument, the path to the resource you want to fetch. It returns a Promise
that resolves to the Response
to that request, whether it is successful or not. You can also optionally pass in an init
options object as the second argument (see Request
).
Once a Response
is retrieved, there are a number of methods available to define what the body content is and how it should be handled (see Body
).
You can create a request and response directly using the Request()
and Response()
constructors, but you are unlikely to do this directly. Instead, these are more likely to be created as results of other API actions (for example, FetchEvent.respondWith()
from service workers).
Note: Find out more about using the Fetch API features in Using Fetch, and study concepts in Fetch basic concepts.
Browsers have started to add experimental support for the AbortController
and AbortSignal
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.
WindowOrWorkerGlobalScope.fetch()
fetch()
method used to fetch a resource.Headers
Request
Response
Body
Specification | Status | Comment |
---|---|---|
Fetch | Living Standard | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 42 | 14 | 39
|
No | 29
|
10 |
Streaming response body | 43 | 14 | Yes
|
No | 29 | 10 |
Support for blob: and data: | 48 | No | ? | No | ? | ? |
referrerPolicy | 52 | No | 52 | No | 39 | 11.1 |
signal | 66 | 16 | 57 | No | 53 | 11.1 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 42 | 42 | 14 | 39
|
? | 10 | ? |
Streaming response body | 43 | 43 | 14 | No | No | 10 | ? |
Support for blob: and data: | 43 | 43 | No | ? | ? | ? | ? |
referrerPolicy | 52 | 52 | No | 52 | 39 | No | ? |
signal | 66 | 66 | 16 | 57 | 53 | 11.1 | No |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API