This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.
The GlobalFetch
interface of the Fetch API contains the GlobalFetch.fetch()
method used to start the process of fetching a resource.
GlobalFetch
is implemented by both Window
and WorkerGlobalScope
— meaning that it will be available in pretty much any context you might want to fetch resources in.
A fetch()
promise will reject with a TypeError
when a network error is encountered, although this usually means permissions issue or similar. An accurate check for a successful fetch()
would include checking that the promise resolved, then checking that the Response.ok
property has a value of true
. An HTTP status of 404 does not constitute a network error.
GlobalFetch
is controlled by the connect-src
directive of Content Security Policy rather than the directive of the resources it's retrieving.
Properties
None.
Methods
GlobalFetch.fetch()
- Starts the process of fetching a resource.
Examples
In our basic fetch example (run example live) we use a simple fetch call to grab an image and display it in an <img>
tag. You'll notice that since we are requsting an image, we need to run Body.blob
(Response
implements body) to give the response its correct MIME type.
var myImage = document.querySelector('.my-image'); fetch('flowers.jpg').then(function(response) { return response.blob(); }).then(function(response) { var objectURL = URL.createObjectURL(response); myImage.src = objectURL; });
Specifications
Specification | Status | Comment |
---|---|---|
Fetch The definition of 'GlobalFetch' in that specification. |
Living Standard | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 42.0 | 39 (39) 34[1] |
Not supported | 29 28[1] |
Not supported |
Streaming response body | 43.0 | ? | ? | ? | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | Not supported | 42.0 | Not supported | Not supported | Not supported | Not supported | Not supported | 42.0 |
Streaming response body | Not supported | 43.0 | ? | ? | ? | ? | ? | 43.0 |
[1] This API is implemented behind a preference.