DEV Community

Discussion on: Why is there such profound and intense hatred towards jQuery in the WebDev world?

Collapse
 
skhmt profile image
Mike

The pure JS alternatives to these are so unpleasant that most devs won't even try to recall it, trust me!

// GET
const response = await fetch('/foo')
// POST
const response = await fetch('/foo', {method:'POST', body:JSON.stringify({x:100, y:200, z:300})}
Collapse
 
prahladyeri profile image
Prahlad Yeri • Edited

While fetch is wonderful, it isn't a drop in replacement for jQuery's $.get and $.post! It has very different behavior - it doesn't save cookies in a get/post request for example, so it won't work in cases where authentication is required.

fetch also lacks compatibility with older browser versions (such as IE9 and earlier). The compatible way of doing AJAX in pure JS is this (which is relatively less elegant):

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        alert(xmlHttp.responseText);
    }
}
xmlHttp.open("post", "server.php"); 
xmlHttp.send(formData);