JavaScriptEasy
Explain AJAX in as much detail as possible
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
AJAX (Asynchronous JavaScript and XML) facilitates asynchronous communication between the client and server, enabling dynamic updates to web pages without reloading. It uses techniques like XMLHttpRequest or the fetch() API to send and receive data in the background. In modern web applications, the fetch() API is more commonly used to implement AJAX.
Using XMLHttpRequest
js
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error("Request failed: " + xhr.status);
}
}
};
xhr.open("GET", "https://jsonplaceholder.typicode.com/todos/1", true);
xhr.send();
Using fetch()
js
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => console.log(data))
.catch((error) => console.error("Fetch error:", error));
