Every once and a while I hit a button on my web application and I get no response. The “button” initiates an ajax call. My guess is that there is heavy web traffic…and it times out or similar. Is there a maximum wait time for ajax responses from the server? Is there a way to log this behavior. Here is my ajax code.
var Ajax = {
createAjaxObject: function()
{
var request;
try
{
request = new XMLHttpRequest();
}
catch(error)
{
try
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(error)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(error)
{
request = false;
}
}
}
return request;
},
useAjaxObject: function( path, param, ajax_func, html_div )
{
var object = new Ajax.createAjaxObject();
object.open( "POST", path, true );
object.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
object.setRequestHeader( "Content-length", param.length );
object.setRequestHeader( "Connection", "close" );
object.onreadystatechange = function()
{
if( this.readyState === 4 )
{
if( this.status === 200 )
{
ajax_func( this.responseText, html_div );
}
else
{
Ajax.repeatUseAjaxObject( path, param, ajax_func, html_div );
return false;
}
}
};
object.send( param );
return true;
},
repeatUseAjaxObject: function( path, param, ajax_func, html_div )
{
var state = false,
count = 1;
while(state === false && count <= 5)
{
state = Ajax.useAjaxObject( path, param, ajax_func, html_div );
if( count !== 1 )
{
alert( 'Ajax Object Use Failed ');
}
count++;
}
}
Sometimes the response could be of the wrong expected type, or the server is returning an error status code/page instead of the actual requested information. A good way to check the full response (or if there really is a lack of a response) is with a good HTTP traffic proxy debugger such as:
Fiddler
http://fiddler2.com/fiddler2/ (Free)
or Charles
http://www.charlesproxy.com/
They will give you full information about the life cycle of each web request. As far as minimum or maximum times, generally the server has a setting for the timeout of any given request.
Client Site Timeout: From the jQuery Api… Not sure since you’re rolling your own.
Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent.