Thursday, December 28, 2006

[Ajax] To abort querying server in some conditions

In some conditions, you will need to abort Ajax to query server immediately.
XMLHttpRequest object provides a function, abort(), to let you accomplish this goal.
The syntax is as following:

//myAjax is a XMLHttpRequest object
myAjax.abort();

However, you might use some 3rd party script to enhance the convenience of constructing Ajax request. For example, we used popular Prototype.js created by ( Sam Stephenson - web site: http://prototype.conio.net/) to approach it.

Prototype.js provides a method to return the XMLHttpRequest object.

var myAjax = new Ajax.Request(url, {method:'post', postBody:PostArgument , onComplete:ResponseFunctionName});
AjaxGetServerMore = myAjax.transport;

In some conditions that you define, you can use abort() method to abort querying server, such as setting time-out period.

AjaxGetServerMore.abort();

[Ajax] Ajax IE Caching Issue

If you have a task that needs to periodically query server through Ajax, you might suffer from cache in IE. The solution resolving this problem is quite easy.
It is to add ‘?random=’ + Math.random(); to the end of all our requests.