Member-only story

Javascript Fetch API: The XMLHttpRequest evolution

Francisco Igor
4 min readSep 14, 2019

--

The Javascript Fetch API is the current standard to make asynchronous HTTP Requests. To anyone who used XMLHttpRequest and Jquery.ajax(), this new API provides more powerful and flexible features.

The main features of the Javascript Fetch API are:

  • Generic definition of Request and Response objects
  • More control over request properties and content
  • More details about response properties and content.
  • Interaction with other Web APIs (for example: Cache API, Blob API)
  • Better control and setup for CORS-related requests
  • Implemented for Web pages as window.fetch() and for Web workers withWorkerGlobalScope.fetch()

What’s different

Implementation of XMLHttpRequest and fetch() differ in a couple of points, in order to allowfetch()better control over the request and response:

  • Promises returned fromfetch()will not fail in any HTTP error status (even HTTP 404 or 500)
  • It will resolve successfully with ok status set to false
  • It will only fail after a network failure or if the request couldn’t be completed.
  • By default,fetch() will not send cookies or credentials

--

--

Francisco Igor
Francisco Igor

Written by Francisco Igor

Programming from the past century. Worked with many programming languages and software development tools for desktop, web and mobile. https://fraigo.github.io/

No responses yet

Write a response