Home arrow AJAX & Prototype arrow Page 3 - How to Handle Ajax Errors

HTTP Status - AJAX

An Ajax request behaves like a JavaScript thread. While the request is going on, execution of the JavaScript code in the flow of the code carries on. How do you solve possible conflicts and the resulting errors?

TABLE OF CONTENTS:
  1. How to Handle Ajax Errors
  2. Server or Transmission Line is not functioning
  3. HTTP Status
  4. Thread effect of Ajax Request
By: Chrysanthus Forcha
Rating: starstarstarstarstar / 8
January 21, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

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 & Prototype Articles          >>> More By Chrysanthus Forcha
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

AJAX & PROTOTYPE ARTICLES

- Best AJAX Tutorials for Forms
- The Best AJAX Tutorials
- 8 Great Ajax Tutorials
- Using Ajax and jQuery
- Using Ajax and jQuery with HTML Forms
- Ajax.org Offers Cloud9 IDE for JavaScript
- Java Technologies Provider ICEsoft Releases ...
- Using Recaptcha in AJAX Prototype Framework ...
- Google's Closure Compiler Service API: Addit...
- Installing Google Web Toolkit: Introducing t...
- Google's Closure Compiler Service API: Displ...
- Google's Closure Compiler Service API: Deliv...
- Google's Closure Compiler Service API: the A...
- Google's Closure Compiler Service API: the S...
- Google's Closure Compiler Service API: Optim...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: