Normally, we use GET method to fetch the data from the database. Now, we instantiate an object of 'XMLHttpRequest' to use the AJAX.

var theObject = new XMLHttpRequest();

The XMLHttpRequest object is used to exchange data with a server behind the scenes. So we can say that it is possible to update the portion of a web page without refreshing the whole page.

So we need to send the request to server, we will use two method of XMLHttpRequest object i.e. open() and send().

open() method takes three arguments i.e. method, url and async.
method: type of request
url: server location
async: true/false

theObject.open("GET", "example.php", true);


send() method use to send the request to the server.

theObject.send();


Then we check the ready states and status as we talked about earlier. If you don't know ready states and status please see the these section. It's an important to check whether the response is ready or not. When response is ready then we need to return some status text.


A sample code will look like this,

var theObject = new XMLHttpRequest();
theObject.open("GET", "example.php", true);
theObject.onreadystatechange = function() {
     if (theObject.readyState == 4 && theObject.status == 200) {
          theObject.responseText;
     }
}
theObject.send();

Last Modified: 2 years ago