AJAX
  Home arrow AJAX arrow Page 3 - How to Handle Ajax Errors
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

How to Handle Ajax Errors
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-01-21


    Table of Contents:
  • How to Handle Ajax Errors
  • Server or Transmission Line is not functioning
  • HTTP Status
  • Thread effect of Ajax Request

  • 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


    How to Handle Ajax Errors - HTTP Status
    ( Page 3 of 4 )

     

    When you are working with HTTP servers you should expect the HTTP errors or status information given below. These errors/status messages can occur when the transmission line and the server are okay. 

    200: OK

    This means the request has succeeded. The client's request was successfully received, understood, and accepted.  

    404: Not Found

    This means the server has not found anything matching the Request- URL.  

    I have just mentioned two messages here; there are many others. This is just a short list:  

    200: "Ok"

    301: "Moved Permanently"

    304: "Not Modified" (page hasn't been modified)

    404: "Not Found"

    403: "Forbidden"

    401: "Unauthorized" (wrong password)  

    These messages are sent by the server. The Ajax object has two properties to indicate these error/status conditions. The properties are the status and the statusText properties. The status property holds a number. For example, if the request was successful, the status property will hold the number 200; if it did not find the item (i.e. if the request failed), it will hold the number 404.

    The statusText properties holds the only the text corresponding to a number. For example, if the request was successful, the statusText property will hold the text “OK”; if it did not find the item (i.e. if the request failed), it will hold the text "Not Found." 

    You can use either of these two properties to check error or status conditions. Remember that this is applicable when the transmission line and the server are all right. Note: most of the time they are all right; so it is these HTTP status messages that always indicate errors. If your script does not function, it will also indicate that. 

    The most important HTTP status code is 200: "Ok." This indicates that the request was accepted by the server. The number is indicated by the Ajax object status property, while the text, “OK,” is indicated by the Ajax object statusText property. The following code segment shows how to use the status number 200 for error checking.  

    xmlHttp.onreadystatechange=function()

    {

      if (xmlHttp.readyState == 4)

    {

      if (xmlHttp.status == 200)

     {

      pageDoc = xmlHttp.responseText;

     }

    }

      else

     {

    //report error – to the client.

     }

    }

     

    This code segment is the modified version of the onreadystatechange method of the Ajax object. Before I explain the code, let me first make the distinction between the readyState being equal to 4 and the status being equal to 200. When the readyState property is equal to 4, it means that the transaction is complete; that is, something has been downloaded. However, what has been downloaded may not be the Ajax text you were expecting. It may be an error message from the server (in the form of an HTML document) if your script was not functioning (had bugs); I mentioned this above.

    When the status property is equal to 200, it means that the server received your request successfully, understood, and accepted it; it does not mean that the server has sent to the client, what the client requested. On the other hand, when the readyState property is equal to 4 it means that the client has received something, but not necessarily what you requested.  

    You need both properties to be sure that you have downloaded what you wanted, as I explain below with the code segment above.  

    Inside the definition of the onreadystatechange method above, you have an if-statement. This statement checks to see if the readyState is 4. If it is, it means that something has been downloaded to the client computer. In order to be sure that what is downloaded is what we wanted, we have to test to see if the status is 200. If it is 200, then we received what we wanted, so we go on to do what we want to do with the downloaded text; if it is not 200, then we issue an error message. 

    Now, you may make this comment: the status number 200 means that the server received your request successfully, understood, and accepted it; this does not mean that the server sent anything back to the client. How can we be sure that the client (you) received what he wanted? This is a sensible question. However, what you should know is that, almost all the time, when the server receives the request, understands and accepts it, the client receives the correct reply (text). I will not explain the details of that in this article. 

    So, the above code segment is the minimum you should have in your Ajax procedure. 



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek