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

Thread effect of Ajax Request - 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
 

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, below the statement of the Ajax request. You may want the statements below the Ajax request to use the result (downloaded text) of the Ajax request. The problem is, you may never know when the request will be complete (when the text is completely downloaded). By the time the request is complete, the statements below the request statement would have been executed (without the downloaded text). 

If it is possible for you to use the Ajax result (downloaded text) in the onreadystatechange method of the Ajax text, to do so. This is the best way to avoid the conflict.  

If you really must use the Ajax result in the statements below that of the Ajax request, then you should have a while-loop below the Ajax request that will be looping and checking to see if the text has been downloaded, before execution of the statements below the Ajax request continues. The xmlHttp.onreadystatechange method should have a statement to indicate to the while-loop if the required result has been received. 

Response is taking longer than it should 

Here I show you haw to handle the situation when the response is taking longer than it should. The thing to do here is to abort the Ajax request when you believe it has taken too long. You will need to determine, however, how long is too long.

The Ajax object has the method called abort(). This function is used to abort the Ajax request. When the request is aborted, the readyState property is reset to zero and nothing is downloaded; you do not need to worry about the readyState or status properties. The following code segment, typed below the Ajax procedure, aborts the Ajax request after five seconds:

 

function abortAjax()

{

xmlHttp.abort();

}

 

setTimeout("abortAjax()", 5000);

 

Conclusion

It is not possible to give a clear-cut answer to any of the questions asked in the introduction of the article. There are conditions under which, if you click the Reload button, you will get what you want. There are also conditions when this will not be helpful. Always have the readyState and status property. This will clarify many problems. Do not forget the thread effect of the Ajax request. I showed you in the last section above how to avoid possible conflict.



 
 
>>> 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: