Open In App

What is XMLHTTPRequest Object ?

XMLHTTPRequest object is an API which is used for fetching data from the server. XMLHTTPRequest is basically used in Ajax programming. It retrieve any type of data such as json, xml, text etc. It request for data in background and update the page without reloading page on client side. An object of XMLHTTPRequest is used for asynchronous communication between client and server. The $.ajax() method is used for the creation of XMLHTTPRequest object.

The $.ajax() does following steps in background:



Below we will see how to create XMLHTTPRequest object with $.ajax() method: 

Syntax: 



var XHR = $.ajax({configs});

Example: 




// Example showing how XMLHTTPRequest object created
var XMLO = $.ajax({
 
    // Our sample URL to make the request
 
    // type of Request
    type: "GET"
});

Now we will see the data-type of data that we can retrieve from the server and what pre-processor we have for respective data-type.

Data-type: These are the data-type for we can request to the server. The available data types are text, xml, html, script, jsonp and json. On the basis of these data type, we specify the pre-processor to the response before handling it to the handler of XMLHTTPRequest object. Following are the pre-processor to the specified data-type:

Properties: XMLHTTPRequest object have many useful class properties which helps in the flexible handling of response. The XMLHTTPRequest object properties are:

Example:




// Demonstrating Properties of XMLHTTPRequest object
<script>
 
var xmlObj = $.ajax({
 
    // Our sample url to make request
 
    // type of Request
    type: "GET"
});
 
xmlObj.always(function(a, b, c) {
    console.log(" # Status of request is : ", c.status);
    console.log(" # readyState of request is : ", c.readyState);
    console.log(" # statusText of request is : ", c.statusText);
    console.log(" # All Response Headers of request is : ",
           c.getAllResponseHeaders);
 });
</script>

Output:

# Status of request is : 200
# readyState of request is : 4
# statusText of request is : success
# All Response Headers of request is : function(){return h?p:null}

Below is the example of some properties of XMLHTTPRequest object:

Methods: As we know XMLHTTPRequest make asynchronous communication and as a result it returns promise. We have many promise method of the jQuery XMLHTTPRequest object. Available promise methods are: 

Below is the example of some of the method of XMLHTTPRequest object:

Example: 




// Example demonstrating methods of XMLHTTPRequest
// object
<script>
 
var xmlObj = $.ajax({
 
    // Our sample url to make request
     
    // type of Request
    type: "GET",
});
 
xmlObj.then(function(){
    console.log(" #Then is resolved ");
});
 
xmlObj.fail(function(){
    console.log(" #Fail is resolved ");
});
 
xmlObj.always(function(){
    console.log(" #Always is resolved ");
});
 
xmlObj.done(function(){
    console.log(" #Done is resolved ");
});
         
</script>

Output: 

#Always is resolved 
#Done is resolved 
#Then is resolved

Article Tags :