AJAX - The Complete Reference

(avery) #1

PART II


Chapter 6: Networking Considerations 235


Retry depends on timeout or, as shown later, server errors, so the _timeoutRequest()
function examines the retries flag from the request options object:

/* do we need to retry? */
if (request.retries)
AjaxTCR.comm._retryRequest(request);
else
request.onTimeout(request); /* invoke any timeout callback */

If this option is set, the private method _retryRequest()is invoked, otherwise the timeout
is performed. In this function, the number of retries is tracked to determine if the request
should be re-invoked, if not the final timeout is fired. In either case any user bound
callbacks are issued.

_retryRequest : function (request) {
/* up our retry count */
request.retryCount++;

/* make sure we aren't done retrying */
if (request.retryCount <= request.retries)
{
AjaxTCR._makeRequest(request);
request.onRetry(request);
}
else /* stop trying and perform callback */
request.onTimeout(request);
}

The example at http://ajaxref.com/ch6/simpleretry.html expands upon the previous
timeout example, showing that it will retry three times, as shown in Figure 6-2.

Handling Server Errors


It is certainly possible that the network is behaving but the server is not. Maybe the request
was issued incorrectly or maybe the server crashed or has ended up in a bad state. Appendix
B provides an in-depth discussion of HTTP and shows that there are a whole range of
possible error codes that might be returned, such values are summarized in Table 6-1.

Status Code Group Category Meaning
1XX Informational Request was received and processing
continues.
2XX Successful Request was received and executed.
3XX Redirection Further action, potentially elsewhere, is
required to complete request.
4XX Client Error The request was incorrect or malformed.

5XX Ser ver Error The ser ver failed to fulfill the request.

TABLE 6-1 HTTP 1.1 Response Code Groups
Free download pdf