Open In App

What is Ajax ?

Imagine browsing a website and being able to submit a form, load new content, or update information without having to refresh the entire page. That's the magic of AJAX. Asynchronous JavaScript and XML (AJAX) is a web development technique that allows web pages to communicate with a web server asynchronously, meaning it can send and receive data in the background without interfering with the user's interaction on the page.

ajax-copy

This dynamic approach to web development has transformed the way we interact with websites, making them more responsive, interactive, and user-friendly.

History of AJAX

The roots of AJAX date back to the early days of the internet when developers started exploring ways to enhance the user experience on web pages. However, it wasn't until 2005 that Jesse James Garrett coined the term "AJAX" in his seminal article, describing a set of technologies including HTML, CSS, JavaScript, XML, and the XMLHttpRequest (XHR) object. These technologies laid the foundation for AJAX, allowing developers to create web applications that could asynchronously exchange data with a web server without requiring a full page reload. Since then, AJAX has become a cornerstone of modern web development, powering many of the dynamic and interactive features we see on the web today.

Different Ways of Using AJAX

At the heart of AJAX lies the XMLHttpRequest (XHR) object in JavaScript, which serves as the engine driving asynchronous communication between the client and server. The basic syntax of an AJAX request involves creating an XHR object, specifying the request method (such as GET or POST) and URL, and handling the response asynchronously. Here's a simplified example:

let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Code to handle the response
}
};
xhttp.open("GET", "example.com/data", true);
xhttp.send();

Sending a GET Request

we can use AJAX to fetch data from a server using a GET request:

let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "example.com/data", true);
xhttp.send();

Sending a POST Request

Here, we send data to a server using a POST request:

let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("Data sent successfully");
}
};
xhttp.open("POST", "example.com/submit", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("name=John&age=30");

Example: Here's a simple example of how you can use AJAX (Asynchronous JavaScript and XML) to make a request to a server and retrieve data.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>AJAX Example</title>
    <script>
        function fetchData() {
            let xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (this.readyState == 4 && 
                    this.status == 200) {
                    document.getElementById("data")
                        .innerHTML = this.responseText;
                }
            };
            xhttp.open("GET", 
                       "https://api.example.com/data", true);
            xhttp.send();
        }
    </script>
</head>

<body>

    <h2>Fetch Data</h2>
    <button onclick="fetchData()">
      Fetch Data</button>

    <div id="data"></div>

</body>

</html>

Output:imresizer-1710132418622

Applications Of AJAX

Advantages of AJAX

Limitations of AJAX

Article Tags :