Notice
Recent Posts
Recent Comments
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
관리 메뉴

A Joyful AI Research Journey🌳😊

The pros and cons of using Axios and Fetch API 본문

💻Bootcamp Self-Study Revision✨/JavaScript, jQuery, Ajax

The pros and cons of using Axios and Fetch API

yjyuwisely 2023. 7. 31. 13:37
Polyfills allow web developers to use an API regardless of whether or not it is supported by a browser, and usually with minimal overhead.

Fetch API
Fetch is a built-in browser API for making HTTP requests. It was introduced to replace XMLHttpRequests.
Pros:

  • It is promise-based, which means it's easier to use and read than older APIs like XMLHttpRequest.
  • It is built into most modern browsers, which means you don't have to import or install any libraries to use it.

Cons:

  • Fetch is not supported in Internet Explorer, which means if you need to support this browser, you may need to install a polyfill.
  • By default, Fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session with cookies. To include cookies, you need to set the credentials option.
  • Fetch does not reject the promise on a HTTP error status even if the response is an HTTP 404 or 500. It will only reject on network failure or if anything prevented the request from completing.

Axios
Axios is a promise-based HTTP client for the browser and Node.js. It has a similar API to Fetch, but includes some additional features.
Pros:

  • It performs automatic transforms of JSON data.
  • It has a more powerful API, with features like interceptors and more flexible configuration.
  • It has built-in CSRF protection.
  • It supports the cancellation of requests.
  • It works with older browsers (even Internet Explorer) without the need for a polyfill.
  • Unlike fetch, Axios does reject the promise when the HTTP status is an error status, making error handling simpler.

Cons:

  • It is an additional dependency that you have to add to your project.
728x90
반응형
Comments