Magazines, Books and Articles

Monday, November 16, 2009

A for Ajax, Part 8: The XMLHttpRequest object (Part 2)

Part 2: Aborting a request, caching of dynamic data, handling timeouts - some of the things we need to watch out for when using the XMLHttpRequest object

In Part 1 we saw that using the XMLHttpRequest object is easy; however there are a few things we need to watch out for.

To begin with we modify Country.htm to add a ‘refresh’ button to the country dropdown from the example in Part 1. This results in the following interface:

As in the example in Part 1, the windows.onload event calls the GetAllCountries() function which populates the country dropdown with Country data after the page has loaded. Clicking the refresh button calls the GetAllCountries() function which re-populates the country dropdown.

In this post we will discuss the following questions:
What happens if the user was to click the refresh button before the request from the windows.onload event returned?
What happens if the user was to click the refresh button indiscriminately?
How do we avoid the bad effects of such actions?
How do we cancel a request once it has been sent?
How can we ensure that dynamic data is not cached by the browser?
How can we recover if a request never returns after it has been sent?

By the end of this discussion we should have a fairly robust, reusable library to handle asynchronous GET requests through the XMLHttpRequest object.

You can read the full article here.