AJAX
  Home arrow AJAX arrow Page 2 - Ajax Features Needed to Build Active Client Pages
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
AJAX

Ajax Features Needed to Build Active Client Pages
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-10-14


    Table of Contents:
  • Ajax Features Needed to Build Active Client Pages
  • More About the XMLHttpRequest Object
  • The responseText Property
  • Principles of the Ajax Approach

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Ajax Features Needed to Build Active Client Pages - More About the XMLHttpRequest Object
    ( Page 2 of 4 )

    Before sending data to the server, we have to explain four important properties of the XMLHttpRequest object.

    The onreadystatechange Property

    After a request to the server, we need a function that can receive the data that is returned by the server. The onreadystatechange property stores the function that will process the response from a server. The following code defines an empty function and sets the onreadystatechange property at the same time:


    xmlHttp.onreadystatechange=function()

    {

    //This function processes the response from the server

    }


    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 up. 

     3

    The request is in process.

     4

    The request is complete.

    The status Property

    This property returns the status code of the request (integer), for example, 404 for a failed request, 200 for a successful one, etc. When running your Ajax script online to test for a fully complete and successful Ajax request, look for a readyState value of 4 plus a status code of 200.

    We are going to add two if-statements to the onreadystatechange function to test whether our response is complete (this means that we can get our data) or if an error occurred:


    xmlHttp.onreadystatechange=function()

    {

    if(xmlHttp.readyState==4)

    {

    if(xmlHttp.status==200)

    {

    // Receive the data from the server and do whatever.

    }

    else

    alert(There is a problem with the data at the server);

    }

    }


    In this way you check whether the response is complete. The response may be complete, but there is an error. So inside the block, you check whether the request was successful. If it was not, you give a generalized error message concerning the page as I have given.

    Unfortunately, “xmlHttp.status” may not work with your browser. If that is the case, you will not use this if-else condition. You will still need to receive the data here. The problem now is, how do you detect if an error has occurred?

    Luckily the XMLHttpRequest has an abort() method and the DOM has a timer function. After sending the request, you can set the timer function that will abort the request if the readyState property does not indicate 4, after some expected time. For simplicity I will assume that the readyState property will indicate 4 within a reasonable amount of time and I will not address the error and timing issue anymore in this series.



     
     
    >>> More AJAX Articles          >>> More By Chrysanthus Forcha
     

       

    AJAX ARTICLES

    - PHP AJAX Form Validation
    - Completing a User-Defined CSS Website with P...
    - Create a User-Defined CSS Website with PHP
    - Build A Better Twitter Chat Client Than Cham...
    - Using Division Equations to Make Web Forms S...
    - Using Integer Multiplication to Protect Web ...
    - Using Simple Checksums for Web Form Verifica...
    - Protecting Web Forms with AJAX
    - Using Prototip with AJAX
    - Using Prototip
    - Using the google.load() Method with Google`s...
    - How to Handle Ajax Errors
    - Uncompressing Source Files with Google`s AJA...
    - Using the jQuery Framework with Google`s Aja...
    - Using Google`s Ajax Libraries API




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek