One of the big reasons to use jQuery, for a long time, was how easy it made Ajax. It has a super clean, flexible, and cross-browser compatible API for all the Ajax methods. jQuery is still mega popular, but it’s becoming more and more common to ditch it, especially as older browser share drops and new browsers have a lot of powerful stuff we used to learn on jQuery for. Even just querySelectorAll
is often cited as a reason to lose the jQuery dependency.
How’s Ajax doing?
Let’s say we needed to do a GET request to get some HTML from a URL endpoint. We aren’t going to do any error handling to keep this brief.
jQuery would have been like this:
$.ajax({
type: "GET",
url: "/url/endpoint/",
}).done(function(data) {
// We got the `data`!
});
If we wanted to ditch the jQuery and go with browser-native Ajax, we could do it like this:
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = ajaxDone;
httpRequest.open('GET', '/url/endpoint/');
httpRequest.send();
function ajaxDone() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
// We got the `httpRequest.responseText`!
}
}
}
The browser support for this is kinda complicated. The basics work as far back as IE 7, but IE 10 is when it really got solid. If you wanna get more robust, but still skip any dependencies, you can even use a window.ActiveXObject
fallback and get down to IE 6.
Long story short, it’s certainly possible to do Ajax without any dependencies and get pretty deep browser support. Remember jQuery is just JavaScript, so you can always just do whatever it does under the hood.
But there is another thing jQuery has been doing for quite a while with it’s Ajax: it’s Promise
based. One of the many cool things about Promises, especially when combined with a “asynchronous” even like Ajax, is that it allows you to run multiple requests in parallel, which is aces for performance.
The native Ajax stuff I just posted isn’t Promise-based.
If you want a strong and convenient Promise-based Ajax API, with fairly decent cross-browser support (down to IE 8), you could consider Axios. Yes, it’s a dependency just like jQuery, it’s just hyper-focused on Ajax, 11.8 KB before GZip, and doesn’t have any dependencies of its own.
With Axios, the code would look like:
axios({
method: 'GET',
url: '/url/endpoint/'
}).then(function(response) {
// We got the `response.data`!
});
Notice the then statement, which means we’re back in the Promise
land. Tiny side note, apparently the requests don’t look identical to jQuery on the server side.
Browsers aren’t done with us yet though! There is a fairly new Fetch API that does Promise-based Ajax with a nice and clean syntax:
fetch('/url/endpoint/')
.then(function(response) {
return response.text();
})
.then(function(text) {
// We got the `text`!
});
The browser support for this is getting pretty good too! It’s shipping in all stable desktop browsers, including Edge. The danger zone is no IE support at all and only iOS 10.1+.
If you only need to target IE10 and up, you can also use the ProgressEvent interface to monitor upload/download progress:
More at MDN.
Be advised when using
fetch
– 404s and other non-200 OK responses are not handled as errors:Typically in cases like this where there’s a solidified modern standard whose API is as good as the libraries, I prefer using the modern standard and just polyfilling the rest if possible. That way, once the standard becomes more universal, all you have to do is remove the polyfill rather than refactoring your code. That said, the fetch polyfill only seems to support back to IE 10, so I guess it depends on your use-case.
I wrote this code many many years ago and it’s the one I’ve been using since ajax gain popularity, I’ve never been into jQuery
Examples:
There’s also a polyfill for Fetch by GitHub: https://github.com/github/fetch
That way, browsers that natively support Fetch will use that, and other browsers will use the polyfill.
I came here to link the same thing. Note that the polyfill approach gives you the benefit of the modern API going forward and is much lighter even than Axios. Pull in a Promise polyfill to go with it, and you have support back to IE10 and Safari 6, which though by no means universal certainly covers the lion’s share of what you need in many cases.
Article was worth it just for that line.
For all those cases, I just grab a copy of ki.extend.js – a nifty collection of JS extending the next-to-nothing “framework” ki.js – dig out the AJAX “abstraction”, and be done with it.
Or just dump in the whole package. Whether you use just the ajax part or the full micro-library, you’ll get stuff quickly done.
cu, w0lf.