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.
Concepts and usage
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.
Aborting a fetch
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.
Fetch Interfaces
WindowOrWorkerGlobalScope.fetch()
- The
fetch()
method used to fetch a resource. Headers
- Represents response/request headers, allowing you to query them and take different actions depending on the results.
Request
- Represents a resource request.
Response
- Represents the response to a request.
Fetch mixin
Body
- Provides methods relating to the body of the response/request, allowing you to declare what its content type is and how it should be handled.
Specifications
Specification | Status | Comment |
---|---|---|
Fetch | Living Standard | Initial definition |
Browser compatibility
Desktop | Mobile | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fetch | ChromeFull support 42 | EdgeFull support 14 | FirefoxFull support 39
| IENo support No | OperaFull support 29
| SafariFull support 10.1 | WebView AndroidFull support 42 | Chrome AndroidFull support 42 | Edge MobileFull support 14 | Firefox AndroidFull support 39
| Opera AndroidFull support 29
| Safari iOSFull support 10.1 | Samsung Internet Android ? |
Streaming response body | ChromeFull support 43 | EdgeFull support 14 | FirefoxFull support Yes
| IENo support No | OperaFull support 29 | SafariFull support 10.1 | WebView AndroidFull support 43 | Chrome AndroidFull support 43 | Edge MobileFull support 14 | Firefox AndroidNo support No | Opera AndroidNo support No | Safari iOSFull support 10.1 | Samsung Internet Android ? |
Support for blob: and data: | ChromeFull support 48 | EdgeNo support No | Firefox ? | IENo support No | Opera ? | Safari ? | WebView AndroidFull support 43 | Chrome AndroidFull support 48 | Edge MobileNo support No | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
referrerPolicy | ChromeFull support 52 | EdgeNo support No | FirefoxFull support 52 | IENo support No | OperaFull support 39 | SafariFull support 11.1 | WebView AndroidFull support 52 | Chrome AndroidFull support 52 | Edge MobileNo support No | Firefox AndroidFull support 52 | Opera AndroidFull support 41 | Safari iOSNo support No | Samsung Internet Android ? |
signal | ChromeFull support 66 | EdgeFull support 16 | FirefoxFull support 57 | IENo support No | OperaFull support 53 | SafariFull support 11.1 | WebView AndroidFull support 66 | Chrome AndroidFull support 66 | Edge MobileNo support No | Firefox AndroidFull support 57 | Opera AndroidFull support 47 | Safari iOSFull support 11.1 | Samsung Internet AndroidNo support No |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.