XMLHttpRequest Object

XMLHttpRequest object is the Backbone of AJAX


The XMLHttpRequest Object

XMLHttpRequest object is supported by all modern browsers. This object is used to send & receive data to server & from server in background. It means user can change or update information on web-page without refreshing or reloading the complete page.


How to Create XMLHttpRequest Object

New versions of all browsers like IE, FireFox, Chrome etc have pre defined XMLHttpRequest object. Below is the syntax to create XMLHttpRequest object :

VariableName = new XMLHttpRequest();

But on the other hand Old versions of IE cannot support XMLHttpRequest object. They use an ActiveX Object. Below is the syntax of ActiveX object :

variable = new ActiveXObject("Microsoft.XMLHTTP");

So to execute program of AJAX on browser, It is very important for user or developer to check compatibility of either XMLHttpRequest or ActiveX object

var xhttp;

if (window.XMLHttpRequest) 
{
    xhttp = new XMLHttpRequest();
} 

else 
{
    // code for IE6, IE5
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

Where to Upload Web Page & Other Related Files

It is very important to know that where we have to upload all the files related with web page and ajax. Updated browsers do not support to allow accessing files from other domains due to security reason. This means that both the web page and all related files must be on the same domain and on the same server. The examples on W3Professors, all files and XML files located on same domain. If anyone tries to use the examples then he/she has to upload all related filed on their own and same server.


How to Handle Old Versions of Browser

If you have old version of any browse like IE (IE5 and IE6), then XMLHttpRequest object will work properly on it. So to deal with these versions, developers has to use ActiveX Object in place of XMLHttpRequest object

if (window.XMLHttpRequest) 

{
    // Below line for updated browsers
    xmlhttp = new XMLHttpRequest();
 } 

else 

{
    // below line for old versions browsers. Like IE
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

XMLHttpRequest Object Methods

Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method,url,async,user,psw) Specifies the request

method: the request type GET or POST
url: the file location
async: true (asynchronous) or false (synchronous)
user: optional user name
psw: optional password
send() Sends the request to the server
Used for GET requests
send(string) Sends the request to the server.
Used for POST requests
setRequestHeader() Adds a label/value pair to the header to be sent

XMLHttpRequest Object Properties

Property Description
onreadystatechange Defines a function to be called when the readyState property changes
readyState Holds the status of the XMLHttpRequest.
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
status Returns the status-number of a request
200: "OK"
403: "Forbidden"
404: "Not Found"
For a complete list go to theHttp Messages Reference
statusText Returns the status-text (e.g. "OK" or "Not Found")