Making XHR Requests
A fundamental functionality of modern web pages and applications is requesting additional resources or data from remote servers. Every browser provides interfaces to execute these so-called XHR requests. Those interfaces can be used from JavaScript. As we can see in the following code examples, jQuery, compared to vanilla.js, lets us write clean and self-explanatory code:
// Vanilla.js const request = new XMLHttpRequest(); request.open("POST", "/example/api", true); request.onreadystatechange = function() { if (request.readyState != 4 || request.status != 200) return; console.log("Successful XHR!"); }; request.send("example=payload");
In comparison to the preceding snippet, the code for making calls to a server is much clear and readable in jQuery. It is more readable in the sense that it is very clear and understandable regarding what exactly the function needs as parameters and what it is going to do. Let's have...