Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is ajax in JavaScript

What is AJAX?

AJAX stands for Asynchronous JavaScript and XML. Don't be scared off by the daunting name, I promise to break it down for you. The key word in AJAX is "Asynchronous". This means that the code allows you to send and receive data from a server without having to refresh your webpage.

Imagine you're having a conversation with a friend. You wouldn't want the whole world to stop while you wait for your friend's response, right? That's exactly what AJAX does for your webpage. It allows you to send a message (request data) and continue with other tasks as you wait for the response.

The Anatomy of AJAX

AJAX operates on four major components:

  1. A browser built-in XMLHttpRequest object (for requesting data from a web server)
  2. JavaScript and HTML DOM (to display or use the data)
  3. A web server
  4. XML or JSON as the data format

The XMLHttpRequest object is like a messenger. It carries your request to the server and brings back the response.

Let's See AJAX in Action

Below is a simple example of how AJAX works.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       document.getElementById("demo").innerHTML = xhttp.responseText;
    }
};
xhttp.open("GET", "https://jsonplaceholder.typicode.com/posts", true);
xhttp.send();

In the above example, we first created an XMLHttpRequest object (var xhttp = new XMLHttpRequest();). We then defined a function to be executed when the server response is ready (xhttp.onreadystatechange = function() {...}).

The readyState property holds the status of the XMLHttpRequest. 4 means the response is ready and status of 200 means "OK".

Then, we used the open() method to specify the type of request (GET), the server URL, and set the request to asynchronous (true). Finally, we sent the request with the send() method.

When the server response is ready, the function will insert the response into an HTML element with id "demo" (document.getElementById("demo").innerHTML = xhttp.responseText;).

The Power of AJAX

The real power of AJAX lies in its ability to update a web page without a full refresh. This makes your application more seamless and enhances user experience.

Imagine you're using a music streaming app. You wouldn't want the music to stop every time you rate a song or add it to your playlist, right? AJAX makes it possible for such operations to happen in the background without interrupting your music listening experience.

Conclusion

Just like how a well-conducted orchestra makes beautiful music, AJAX is the conductor that ensures different parts of your web application work in harmony. It's the invisible power that makes your browsing experience smooth and enjoyable.

While AJAX might seem complex at first, with practice, you will find it an indispensable tool in your web development toolkit. So go ahead, play around with AJAX, and watch your web applications come to life.