Vous êtes sur la page 1sur 8

Starting with Ajax

AJAX stands for Asynchronous JavaScript and


XML(Xentendable Markedup Language).

In the case of server side scripting like ASP and PHp


we will post the data to the server and then the result
is processed and delivered to the user on te next page.

If we want the result to be displayed in the same page


or the server side processing should be made
available on the same page , then we have to make
use of AJAX.

e.g.

We have to create a form in which we enter the


enrollment number of the student and we want to
information of that student to the fetched from the
database and deliver to the user on the same page.

XMLHttpRequest

This object is provided by the JavaScript for handling


the HTTP requests.
But note that this object class differ for different
browser we use.

If we are using
Firefox, Opera 8.0+, Safari,IE 8

objectname=new XMLHttpRequest();

if we are using

Internet Explorer 6.0

objectname=new
ActiveXObject("Msxml2.XMLHTTP");

If we are using IE 5.0 and 3.0>

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

In order to resolve all exception we


can write the following block of code

function functioname()
{
var ob;

try
{
//for IE8

ob=new XmlHttpRequest();

}
catch(e)
{
try
{

//for IE 6.0

ob=new
ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{

try
{
//for IE 5.0

ob=new
ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser
doesnot support Ajax.");
}
}
}

The onreadystatechange Property


onreadstatechange property is used to check
whether the request is processed by the server or
not.
For this purpose this property is assigned a
function which will process the handling of
request sent to the server.

The general form is

objectname.onreadystatechage=function()
{
//code which will be used for handling
the request
}
The readyState Property
The readyState property holds the status of the server's
response.
Each time the readyState changes, the onreadystatechange
function will be executed.
Here are the possible values for the readyState property:
State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete

In our case the actually code will be applicable when the


readyState value is 4.
The general form is

objectname.onreadystatechage=function()
{
if(readyState==4)
{
//code which will be used for
handling the request

}
}

The responseText Property


The data sent back from the server can be retrieved with the
responseText property.
This is the outcome of the request which is sent to the
server.
The general form is

objectname.onreadystatechage=function()
{
if(readyState==4)
{
//code which will be used for
handling the request
..... = objectname.responseText;
}
}

Sending the request to the server

In order to send the request to the server , we will make use


of the open() function ,
The general form is ,

open(method,"filename",true);
The open() function specify ,

1. filename : This is the name of the file to which the


request is to be sent.

2. method : This will determine how the parameters


will be passed , GET or POST.

After open() function we have to call the send() function


which will finally transfer the request to the server.

Vous aimerez peut-être aussi