JS AJAX
In this page:
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function() {
// xhr.responseText
};
xhr.send();
What AJAX Means
AJAX stands for Asynchronous JavaScript and XML, though modern AJAX overwhelmingly exchanges JSON rather than actual XML -- the core idea is a background request to a server that updates part of a page without triggering a full reload, the same technique fetch() implements in a more modern way.
उदाहरण: What AJAX Means
// AJAX: background request updates part of a page, no full reload
fetch("data.php")
.then(response => response.json())
.then(data => console.log(data)); // modern AJAX exchanges JSON, not XML
XMLHttpRequest: The Original AJAX API
Before fetch() existed, XMLHttpRequest was the only way to make a background HTTP request from JavaScript -- it works by creating an XMLHttpRequest object, configuring an event listener for when the response arrives, and calling .send() to actually fire off the request.
उदाहरण: XMLHttpRequest: The Original AJAX API
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
const xhr = new XMLHttpRequest();
// Listen for the `load` event on `xhr` and run the handler when it fires
// Listen for the `load` event on `xhr` and run the handler when it fires
xhr.addEventListener("load", () => console.log(xhr.responseText));
// Call `xhr.open("GET", "data.php")`
// Call `xhr.open("GET", "data.php")`
xhr.open("GET", "data.php");
// Call `xhr.send()`
// Call `xhr.send()`
xhr.send();
Why fetch() Largely Replaced XMLHttpRequest
fetch() returns a Promise, integrating naturally with .then()/.catch() chains and async/await, while XMLHttpRequest relies on manually checking readyState inside an event callback -- fetch's cleaner syntax is why it has become the default choice for new AJAX-style code.
उदाहरण: Why fetch() Largely Replaced XMLHttpRequest
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
const xhr = new XMLHttpRequest();
// Assign `() => { if (xhr.readyState === 4) console.log(xhr.responseText); }` to `xhr.onreadystatechange`
// Assign `() => { if (xhr.readyState === 4) console.log(xhr.responseText); }` to `xhr.onreadystatechange`
xhr.onreadystatechange = () => { if (xhr.readyState === 4) console.log(xhr.responseText); };
// Call `xhr.open("GET", "data.php")`
// Call `xhr.open("GET", "data.php")`
xhr.open("GET", "data.php");
// Call `xhr.send()`
// Call `xhr.send()`
xhr.send();
// Call `fetch("data.php").then(r => r.json()).then(data => console.log(data))`
// Call `fetch("data.php").then(r => r.json()).then(data => console.log(data))`
fetch("data.php").then(r => r.json()).then(data => console.log(data));
Where AJAX Fits in a Web Application
AJAX-style requests (via fetch or XMLHttpRequest) power live search, form submission without a reload, infinite-scrolling feeds, real-time dashboard updates, and any interaction where reloading the entire page would feel slow or jarring for what is really a small data update.
उदाहरण: Where AJAX Fits in a Web Application
// Define the function `search` taking `term`
// Define the function `search` taking `term`
function search(term) {
fetch(`search.php?term=${term}`).then(r => r.json()).then(results => console.log(results));
}
// Call `search("widgets")`
// Call `search("widgets")`
search("widgets");
AJAX and Server-Side Languages
AJAX requests are language-agnostic on the client side -- JavaScript sends the request the same way regardless of what powers the server responding to it, whether that is PHP, Node.js, Python, or any other backend -- as long as the server returns an appropriate response the JavaScript can parse.
उदाहरण: AJAX and Server-Side Languages
fetch("data.php").then(r => r.json()).then(data => console.log(data)); // could be PHP, Node.js, Python, etc.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML