phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


AJAX - Client Side: Handling Server Response

Tag: Ajax, Client side, XMLHttpRequest, Server Response   Category:
post: 17 Mar 2008 read: 529


AJAX Client Side Step By Step Tutorial - part 8: In this post, we will see how to handle response from server. We know a property named "readyState". This property have possible value like:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complate
We will use this property to build little fun for your client. You want to show the AJAX steps at browser

The method like this.


function handleRequestStateChange()
{
  myDiv = document.getElementById("myDivElement");

  if(xmlHttp.readyState == 1)
  {
    myDiv.innerHTML += "Request status: 1 (loading) <br/>";
  }
  else if (xmlHttp.readyState == 2)
  {
    myDiv.innerHTML += "Request status: 2 (loaded) <br/>";
  }
  else if (xmlHttp.readyState == 3)
  {
    myDiv.innerHTML += "Request status: 3 (interactive) <br/>";	
  }
  else if (xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
	{
	  try
	  {
	    response = xmlHttp.responseText;
		myDiv.innerHTML += "Request status: 4 (complete). Server said: <br/>";
		myDiv.innerHTML += response;
	  }
	  catch(e)
	  {
	    alert("Error reading the response: " + e.toString());
	  }
	}
	else
	{
	  alert("Problem retrieving data:\n" + xmlHttp.statusText);
	}
  
  
  }
  
}

In this code, in every steps, we will write to browser what happen. For see result of our practice, please read next post.



Series this article:
AJAX - Client Side: Techniques Introduction
AJAX - Client Side: JavaScript Overview
AJAX - Client Side: JavaScript and Document Object Model
AJAX - Client Side: JavaScript Events and the DOM
AJAX - Client Side: JavaScript, DOM, and CSS
AJAX - Client: XMLHttpRequest Object
AJAX - Client Side: Initiating Server Request Using XMLHttpRequest
AJAX - Client Side: Handling Server Response
AJAX - Client Side: Making Asynchronous Calls with XMLHttpRequest
AJAX - Client Side: Working with XML Structure
AJAX - Client Side: Processing XML Data use XMLHttpRequest
AJAX - Client Side: Simple Application for Process XML data

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting