JavaScript Fetch

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • promise = fetch(url).then(function(response) {})
  • promise = fetch(url, options)
  • promise = fetch(request)

Parameters

OptionsDetails
methodThe HTTP method to use for the request. ex: GET, POST, PUT, DELETE, HEAD. Defaults to GET.
headersA Headers object containing additional HTTP headers to include in the request.
bodyThe request payload, can be a string or a FormData object. Defaults to undefined
cacheThe caching mode. default, reload, no-cache
referrerThe referrer of the request.
modecors, no-cors, same-origin. Defaults to no-cors.
credentialsomit, same-origin, include. Defaults to omit.
redirectfollow, error, manual. Defaults to follow.
integrityAssociated integrity metadata. Defaults to empty string.

Remarks

The Fetch standard defines requests, responses, and the process that binds them: fetching.

Among other interfaces, the standard defines Request and Response Objects, designed to be used for all operations involving network requests.

A useful application of these interfaces is GlobalFetch, which can be used to load remote resources.

For browsers that do not yet support the Fetch standard, GitHub has a polyfill available. In addition, there is also a Node.js implementation that is useful for server/client consistency.

In the absence of cancelable Promises you can't abort the fetch request (github issue). But there is a proposal by the T39 in stage 1 for cancelable promises.



Got any JavaScript Question?