JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



AJAX Request

AJAX (Asynchronous JavaScript and XML) is a technique used in web development to create asynchronous web applications. It allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that web pages can be updated without the need for a full page refresh, resulting in a more responsive and interactive user experience.

AJAX requests are used to send and receive data from a web server without the need for a full page refresh. This is achieved using JavaScript and XML (or JSON) to communicate with the server. The data is sent and received in the background, allowing the user to continue interacting with the web page without interruption.

There are several methods for making AJAX requests, including the XMLHttpRequest object, jQuery AJAX, and Fetch API. In this article, we will focus on the XMLHttpRequest object.

XMLHttpRequest Object

The XMLHttpRequest object is a built-in JavaScript object that allows us to make HTTP requests to a web server. It provides a way to send and receive data asynchronously without the need for a full page refresh.

Here is an example of how to make an AJAX request using the XMLHttpRequest object:

<script>
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://example.com/data.json', true);
  xhr.onload = function() {
    if (xhr.status === 200) {
      console.log(xhr.responseText);
    } else {
      console.log('Request failed. Returned status of ' + xhr.status);
    }
  };
  xhr.send();
</script>

In this example, we create a new instance of the XMLHttpRequest object and use the open() method to specify the HTTP method (GET in this case) and the URL of the resource we want to retrieve. We also set the third parameter to true to indicate that the request should be asynchronous.

The onload() method is called when the request completes. If the status code is 200 (OK), we log the response text to the console. Otherwise, we log an error message.

Handling AJAX Responses

When an AJAX request is made, the server will respond with data in a specific format, such as XML or JSON. It is important to handle the response appropriately in order to use the data in our web application.

Here is an example of how to handle a JSON response:

<script>
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://example.com/data.json', true);
  xhr.onload = function() {
    if (xhr.status === 200) {
      var data = JSON.parse(xhr.responseText);
      console.log(data);
    } else {
      console.log('Request failed. Returned status of ' + xhr.status);
    }
  };
  xhr.send();
</script>

In this example, we use the JSON.parse() method to convert the response text into a JavaScript object. We can then use this object in our web application.

Conclusion

AJAX requests are an important part of modern web development. They allow web pages to be updated asynchronously, resulting in a more responsive and interactive user experience. The XMLHttpRequest object is a powerful tool for making AJAX requests, and it is important to handle the response appropriately in order to use the data in our web application.

References

Activity