Skip to main content
Email Password   helpLost your password?

What is AJAX?

AJAX, an acronym for Asynchronous JavaScript and XML, is a web development technique for creating interactive web applications. The intent is to make web pages feel more responsive by exchanging small amounts of data with the server behind the scenes, so that the entire web page does not have to be reloaded each time the user makes a change. This is meant to increase the web page's interactivity, speed, and usability.

Using the XmlHttp Object

An XmlHttp object can be created like this:

<script language="javascript">
  //Global XMLHTTP Request object

var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to 

//a "XmlHttp" variable  

function CreateXmlHttp()
{
 //Creating object of XMLHTTP in IE

 try
 {
  XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch(e)
 {
  try
  {
   XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch(oc)
  {
   XmlHttp = null;
  }
 }
 //Creating object of XMLHTTP in Mozilla and Safari 

 if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
 {
  XmlHttp = new XMLHttpRequest();
 }
}
</script>

Send the request to the server side page

We can send a request to the server page to retrieve the data from the server through the XmlHttp object.

var requestUrl = "WebForm2.aspx" + "?SelectedCountry=" + (selectedCountry);
 CreateXmlHttp();
 
 // If browser supports XMLHTTPRequest object

 if(XmlHttp)
 {
  //Setting the event handler for the response

  XmlHttp.onreadystatechange = HandleResponse;
  
  //Initializes the request object with GET (METHOD of posting), 

  //Request URL and sets the request as asynchronous.

  XmlHttp.open("GET", requestUrl,  true);
  
  //Sends the request to server

  XmlHttp.send(null);  
 }

The above code explains how to send a request to webpage2.aspx with the request object collection. This collection contains the selected country itself.

Process the client request

When the request is recieved on the server side, the server page executes the code and gives the response through the XmlHttp object:

Dim xList As XmlNodeList
Dim xNode As XmlNode
Dim Str As String
xmlDoc.Load(Server.MapPath("CountriesAndStates.xml"))

Dim Country As String = Request.QueryString("SelectedCountry")
Dim query As String = ("/countries/country[@name='" & country & "']")
xlist = xmlDoc.SelectNodes(query)

Response.Clear()
 
For Each xNode In xList
     If xNode.HasChildNodes = True Then
          For Each xN As XmlNode In xNode.ChildNodes
               Str &= xN.InnerText & "-"
          Next
     End If
Next

Response.Clear()
Response.ContentType = "text/xml"
Response.Write(str)
Response.End()

Using the XmlHttp object to retrieve the data

Recieving the response from the server page can be done like this:

The valid list of XMLHttpRequest ready states is listed in the following table:

Value

Description

0

Uninitialized

1

Loading

2

Loaded

3

Interactive

4

Complete

The list of status codes for the HTTP status is given below:

1xx Informational

Request received, continuing process.

2xx Success

The action was successfully received, understood, and accepted.

3xx Redirection

The client must take additional action to complete the request.

4xx Client Error

The request contains bad syntax or cannot be fulfilled.

5xx Server Error

The server failed to fulfill an apparently valid request.

if(XmlHttp.readyState == 4)
 {
  // To make sure valid response is received from the server, 

  // 200 means response received is OK

  if(XmlHttp.status == 200)
  {  
   ClearAndSetStateListItems(XmlHttp.responseText);
  }
  else
  {
   alert("There was a problem retrieving data from the server." );
  }
 }

There are two types of XmlHttp responses for rectrieving the response.

  1. responseText
  2. responseXML

Show the states in the dropdownbox

The responseText is split using the split function and the response is assigned to an array.

Then, the array values are added in the dropdown list.

var stateList = document.getElementById("stateList");
 //Clears the state combo box contents.

 for (var count = stateList.options.length-1; count >-1; count--)
 {
  stateList.options[count] = null;
 }

 var stateNodes = countryNode.split("-");
 //window.alert(stateNodes) 

 var textValue; 
 var optionItem;
 //Add new states list to the state combo box.

 for (var count = 0; count < stateNodes.length; count++)
 {
     textValue = (stateNodes[count]);
  optionItem = new Option( textValue, textValue,  false, false);
  //window.alert(textValue); 

  //stateList.appendChild(textValue); 

  stateList.options[stateList.length] = optionItem;
 }

This is my first article in The Code Project.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalwhile adding maste rpage its not working Pin
emmy 1900
4:23 26 Jul '09  
GeneralRe: while adding maste rpage its not working Pin
sathesh_pandian
0:00 27 Jul '09  
GeneralRe: while adding maste rpage its not working Pin
sathesh_pandian
0:07 27 Jul '09  
GeneralIs there any possible ajax livevalidation for textbox ,combo box in asp.net visual studio 2005... Pin
sathiyarajg
20:24 17 Jun '09  
GeneralRe: Is there any possible ajax livevalidation for textbox ,combo box in asp.net visual studio 2005... Pin
sathesh_pandian
21:23 17 Jun '09  
GeneralSome Assistance Required. Urgently. Pin
ramnamsatya
13:07 22 Apr '09  
Generalhi Pin
balajivarnan
2:44 4 Feb '09  
GeneralRe: hi Pin
sathesh_pandian
23:27 4 Feb '09  
GeneralRe: hi Pin
balajivarnan
20:18 8 Feb '09  
Generalonchange event not fired in Chrome Pin
balajivarnan
3:17 9 Feb '09  
GeneralRe: onchange event not fired in Chrome Pin
sathesh_pandian
23:54 9 Feb '09  
GeneralCan be improved Pin
GilesHinton
1:45 28 May '08  
GeneralRe: Can be improved Pin
sathesh_pandian
22:01 23 Jul '08  
QuestionRE Pin
Swapnil Salunke
0:57 20 May '08  
AnswerRe: RE Pin
Swapnil Salunke
1:27 20 May '08  
AnswerRe: RE Pin
sathesh_pandian
2:02 20 May '08  
GeneralHi Pin
mkumar
5:01 22 Feb '08  
GeneralRe: Hi Pin
sathesh_pandian
22:03 21 Apr '08  
GeneralMasterPage cs file is missing Pin
madhan_genn
3:43 14 Feb '08  
GeneralRe: MasterPage cs file is missing Pin
sathesh_pandian
23:14 21 Apr '08  
Questionrequest problem Pin
J5121982
4:22 5 Sep '07  
AnswerRe: request problem Pin
sathesh_pandian
1:18 8 Sep '07  
GeneralRe: request problem Pin
sathesh_pandian
21:49 21 Apr '08  
Generalsource code problems Pin
Parthasarathy Mandayam
8:40 4 Sep '07  
GeneralRe: source code problems Pin
sathesh_pandian
1:19 8 Sep '07  


Last Updated 22 Jan 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009