JavaScript Fetch Vs Ajax

pain engineer
2 min readApr 12, 2023
from https://unsplash.com/@florianolv

Let’s first take a look at the similarities:

Both use technology to send HTTP requests via the internet.

Support asynchronous requests.

Can handle different types of data formats, such as JSON, XML, HTML, etc.

Can set headers to send additional information.

Support using Promise or async/await to handle asynchronous request responses.

However, Fetch is more modern than Ajax because it is designed based on Promise and provides better readability and maintainability. Additionally, Fetch provides new APIs such as Response and Request objects.

Now let’s take a look at the advantages and disadvantages of Fetch and Ajax:

Advantages of Fetch:

  • More modern and standardized, with built-in Promise API for simplicity and readability.
  • Supports cross-domain requests.
  • Convenient for handling JSON data.
  • Can be configured with more options, such as headers, method, mode, cache, etc.

Disadvantages of Fetch:

  • Poor compatibility, requiring the use of polyfills or special processing.
  • Does not support canceling requests.
  • Does not support progress tracking.

Advantages of Ajax:

  • Good compatibility.
  • Allows for multiple requests.
  • Supports canceling requests and progress tracking.

Disadvantages of Ajax:

  • More cumbersome to operate.
  • Handling cross-domain issues can be a significant problem.
  • Difficult to handle JSON data.

Taking the above into consideration, personally, I prefer Fetch because I believe that its disadvantages will be resolved in the future. However, I still recommend that people learn both, as each has its own strengths and weaknesses, and engineers should not be picky.

Note: This comparison is for JavaScript’s Ajax, not jQuery’s Ajax.

Feel free to vote below for your preferred API and share why.

--

--

pain engineer

web engineer | full stack engineer | An aspiring teenager.