GlobalFetch

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage 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 specification changes.

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.

The fetch() method's parameters are identical to those of the Request() constructor.

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.

JavaScript
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]
No support 29
28[1]
No support
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 No support 42.0 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.

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/globalfetch

API Experimental Fetch GlobalFetch HTTP Interface Networking Reference request