One of the fundamental tasks of any web application is to communicate with servers through the HTTP protocol. This can be easily achieved using Fetch or Axios.
The Fetch API is built-in most modern browsers and provides a fetch() method defined on the window object, while Axios is a specific 3rd party Javascript library you must install manually.
Which method do you usually use and do you think adding a library (around 4.4KB gzip) is worth it for the extra features it brings?
Latest comments (28)
I prefer fetch API as I expect user to have slightly modern browser. I don't want to support outdated browser.
Axios.
API things like,
data
object which I guess good security practice as well.axios.create
.More things.
Also, works really well with TypeScript which other user has mentioned.
Most old browser will not understand fetch API and that is where, axios shines. Axios accommodates fetch API. However i think fetch API makes code look neater and a Lil bit synchronous. If I want compatibility, I'll pick axios else its fetch.
Fair enough 😉
$.getJSON()
$.getScript()
Don't forget
$(selector).load(uri)
.All of these are better than my first AJAX program: I didn't know JavaScript has closures, so that every
XMLHttpRequest
object is a global variable.It's 2006, when everyone was using IE6, I just read a book about AJAX, I haven't heard about Prototype.js, and jQuery existed for less than a year.
I haven't tried
fetch
yet, butaxios
is a delight to use, and I already know what to expect with it.Axios
has more browsers compatibility.Axios
I use Fetch when the project is small or when I know it will not grow much. I prefer to use Axios in projects where requests carry more information since Axios allows you to configure an instance of it.
fetch + node-fetch.
Because fetch is native, and node-fetch has the same syntax, so i can copy paste code from front to back.
For backend: superagent
For frontend: fetch