Important: As of Firefox 52, the methods defined in this mixin have been moved to the WindowOrWorkerGlobalScope
mixin, and other browsers will follow suit. Look to that page for up-to-date details.
The GlobalFetch
mixin 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
, which means that it's available in pretty much any context in which you might wish to fetch resources.
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.
None.
GlobalFetch.fetch()
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 requesting 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; });
Specification | Status | Comment |
---|---|---|
Fetch The definition of 'GlobalFetch' in that specification. | Living Standard | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 42.0 | (Yes) |
39 (39) 34[1] | No support | 29 28[1] | No support |
Streaming response body | 43.0 | ? | ? | ? | ? | ? |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | No support | 42.0 | (Yes) | No support | No support | No support | No support | No support | 42.0 |
Streaming response body | No support | 43.0 | ? | ? | ? | ? | ? | ? | 43.0 |
[1] This API is implemented behind a preference.
© 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/GlobalFetch