The global fetch() method starts the process of fetching a resource from the network, returning a promise which is fulfilled once the response is available.
The promise resolves to the Response object representing the response to your request.
A fetch() promise only rejects when a network error is encountered (which is usually when there's a permissions issue or similar). A fetch() promise does not reject on HTTP errors (404, etc.). Instead, a then() handler must check the Response.ok and/or Response.status properties.
WindowOrWorkerGlobalScope is implemented by both Window and WorkerGlobalScope, which means that the fetch() method is available in pretty much any context in which you might want to fetch resources.
The fetch() method is controlled by the connect-src directive of Content Security Policy rather than the directive of the resources it's retrieving.
Note: The fetch() method's parameters are identical to those of the Request() constructor.
The request method, e.g., "GET", "POST". The default is "GET". Note that the Origin header is not set on Fetch requests with a method of HEAD or GET. (This behavior was corrected in Firefox 65 — see Firefox bug 1508661.) Any string which is a case-insensitive match for one of the methods in RFC 9110 will be uppercased automatically. If you want to use a custom method (like PATCH), you should uppercase it yourself.
Any headers you want to add to your request, contained within a Headers object or an object literal with String values. Note that some names are forbidden.
Note: The Authorization HTTP header may be added to a request, but will be removed if the request is redirected cross-origin.
Tells browsers to include credentials with requests to same-origin URLs, and use any credentials sent back in responses from same-origin URLs. This is the default value.
A string indicating how the request will interact with the browser's HTTP cache. The possible values, default, no-store, reload, no-cache, force-cache, and only-if-cached, are documented in the article for the cache property of the Request object.
Specifies the referrer policy to use for the request. May be one of no-referrer, no-referrer-when-downgrade, same-origin, origin, strict-origin, origin-when-cross-origin, strict-origin-when-cross-origin, or unsafe-url.
The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API.
If the request method is 'GET' or 'HEAD' and the body is non-null or not undefined.
fetch('https://example.com/', {
method: 'GET',
body: new FormData(),
});
If fetch throws a network error.
Examples
In our Fetch Request example (see Fetch Request live) we create a new Request object using the relevant constructor, then fetch it using a fetch() call. Since we are fetching an image, we run Response.blob() on the response to give it the proper MIME type so it will be handled properly, then create an Object URL of it and display it in an <img> element.