ReadyState in Ajax. How it Works?

What is Ajax ?
--------------

AJAX stands for Asynchronous JavaScript and XML. Using Ajax we can retrive the data from server without refreshing the whole page. AJAX Sends an XML Request to the server. The request is sent the JavaScript to fetch the data from server replaces a selected subset of the DOM to update the current page (partly) instead of loading / replacing the entire web page. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously .
Web pages which do not use AJAX, must reload the entire page if the content should change. Some Example Applications which uses AJAX: Google Maps, Gmail and Youtube

Purpose of using AJAX :
-----------------------
The main purpose of using AJAX is to get the information asynchronously from the server and update only that part of the web page where this information fetched needs to be displayed, avoiding refresh of the entire page. This can be done by a JavaScript code in your web application, which uses XMLHttpRequest object to communicate with the server and get the data asynchronously.

The first step is to create and configure the XMLHttpRequest object (which will send the request to server and gets response as XML or HTML or Plain Text) . XMLHttpRequest object can be created by two ways depending upon the browser. 1) ActiveXObject("Microsoft.XMLHTTP"); supported by Internet Explorer. 2) XMLHttpRequest(); Supported by Other Browsers including IE7 & above . The following piece of code illustrates who to create XMLHttpRequest



Step I
------
var httpRequest;

if (window.ActiveXObject) // for Internet Explorer
{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) // for other browsers + IE 7 & above
{
httpRequest = new XMLHttpRequest();
}

Step II
--------
httpRequest.open("GET", url, true);

This statement is used to configure the httpRequest object with URL , Http Method (Post / Get) & Asynchronoue / Syncronous.

Where URL is the Servlet or CGI Script (for server side programming) ,Third Parameter True or False value which decides whether the interaction should be synchronous or asynchronous, where true means asynchronous.

Step III
--------
httpRequest.onreadystatechange = function() {processRequest(); } ; Eventhandler calls the function whenever httpRequest readyState is changed. The function will handle the callback from the server side element.

httpRequest.send(null); This statement to make the call to server.

Now to handle the response from the server which is always in XML form or HTML or Plain Text . The servlet can return the actual data in XML form or the formatted HTML code . This process is done in the JavaScript function, which is called as callback function.

function processRequest()
{
if (httpRequest.readyState == 4)
{
if(httpRequest.status == 200)
{
//process the response
}
else
{
alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
}
}
}

in above function , first we have to check the XMLHttpRequest object is in readyState (ie readeState=4) which indicates that the call is complete. Then check for the status, which is the HTTP status code, value 200 means HTTP is successful.

About readyState
----------------
readyState is a property of XMLHttpRequest object . This property ensures that a server has completed a request and typically, a callback function uses the data from the server to update a Web form or page. It has the value of 0 to 4.

0: The request is uninitialized (before calling open()). // httpRequest.open("GET", url, true);
1: The request is set up, but not sent (before calling send()). // httpRequest.send(null);
2: The request has been sent to the server and is in process
3: The request is in process; often some partial data is available from the response, but the server has not finished with its complete response.
4: The response is complete; you can get the server's response and use it.

XMLHttpRequest object may be called by multiple functions. In that situation, you might want to ensure that a request object is in an uninitialized state (readyState == 0) before making a new requests. This essentially ensures that another function isn't using the object at the same time.
The Next step is to process the response received. The response is stored in responseText of the XMLHttpRequest object. The response is always in the XML form and the object representation of this XML is stored in responseXML of the XMLHttpRequest object. This XML can be parsed in the JavaScript itself using the DOM API to obtain the actual data.

0 comments:

Post a Comment

 
Copyright 2011 Java Online