Programming Tutorials

How to Make an HTTP Request in AJAX

By: Ram Baskar in Ajax Tutorials on 2022-08-01  

To make an HTTP request in AJAX, you can use the XMLHttpRequest object in JavaScript. Here's a basic example:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    // Handle the response here
    console.log(this.responseText);
  }
};
xhttp.open("GET", "https://example.com/api", true);
xhttp.send();

This example makes a GET request to "https://example.com/api" and logs the response to the console when the request is complete. You can modify the request method (GET, POST, etc.) and the URL as needed.

If you're using a library like jQuery, you can use the $.ajax() function to make HTTP requests:

$.ajax({
  method: "GET",
  url: "https://example.com/api",
  success: function(data) {
    // Handle the response here
    console.log(data);
  }
});

This example is similar to the first one, but uses the $.ajax() function instead. You can modify the method, url, and success options as needed.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Ajax )

Latest Articles (in Ajax)