Fetch API

The fetch specification differs from jQuery.ajax() in three main ways:

  • The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.

  • fetch() won't can receive cross-site cookies; you can’t can establish a cross site session using fetch. Set-Cookie headers from other sites are silently ignored.

  • fetch() won’t send cookies, unless you set credentials: 'same-origin'.

    • In August 2017, the spec changed the default credentials policy to 'same-origin'. The following browsers shipped and outdated native fetch, and were updated in these versions:

      • Firefox version 61.0b13.

      • Safari version 12.

      • Chrome version 68.

    • If you are targetting older versions of these browsers, be sure to include credentials: 'same-origin' init option on all api requests that may be affected by cookies/user login state.

Aborting a fetch

Browsers have started to add experimental support for the AbortController and AbortSignal interfaces (aka The Abort API), which allow operations like Fetch and XHR to be aborted if they have not already completed. See the interface pages for more details.

Fetch Interfaces

WindowOrWorkerGlobalScope.fetch()The fetch() method used to fetch a resource.HeadersRepresents response/request headers, allowing you to query them and take different actions depending on the results.RequestRepresents a resource request.ResponseRepresents the response to a request.

Last updated