Wednesday, December 20, 2006

Preventing Cached AJAX Requests

As a developer, you’re probably well aware of all the issues that commonly occur due to cached data. AJAX is not unique when it comes to these issues; in fact, this problem is fairly common. Luckily, there are workarounds, one of which involves JavaScript’s Date object. If you haven’t used this approach for other caching issues, you’ll be pleasantly surprised at how easy it is to implement.

When making a standard HTTP request, the browser caches the web pages that we visit. Query strings are one way to work around this behavior. Therefore, we could easily use a query to our advantage by adding a simple query at the end of an AJAX request. But this will solve only half of the problem—if the query is the same each time, the data can still be cached. Therefore, we need to create a unique query each time a request is made. There are many ways to handle this need, but the one that makes the most sense in this situation is to use time to our advantage—because time is always changing.

The example in Listing 1 takes a uri parameter that will be used to make the request. Once we create the appropriate request object, we then create an instance of the Date object that will be used to create the next variable, called uniqueURI. The uniqueURI starts with the uri parameter; then we use a condition that checks for the index of a question mark, which would symbolize an existing query string. If the query string exists, we simply append an ampersand to the query; otherwise, we append the question mark. For either condition, the uri is followed by a key/value pair of "timestamp=" plus the current time from the Date object. Once we have the uniqueURI, we’re ready to finish making the request.

Creating a Unique URI




function xmlLoader(uri)
{
var request;
if(window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
request = new ActiveXObject("MSXML2.XMLHTTP");
}
var timestamp = new Date();
var uniqueURI = uri+ (uri.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
request.onreadystatechange = callbackMethod;
request.open("GET", uniqueURI, true);
request.send(null);
}




The uniqueURI variable is all it takes to avoid cached requests with AJAX.

6 comments:

tuo said...

In spite of the fact that many critics comment on ed hardy clothing as nothing but tattoo pattrens, being full of unnecessary and useless things, ed hardy clothes is widely acknowledged that ed hardy shirts draws the most attention of both common young people as well as many Hollywood welcomed stars for its punk rock styles. In fact, in today’s world, many ed hardy t-shirtsdesigns are more or less the same with each othre, and ed hardy mens and ed hardy womens , absolutely, brings a totally new shock in the fashion world for its design being out of ordinary ed hardy sunglasses.

tuo said...

You are currently spoilt for choice Polo Shirts On Sale with the selection of current star names to get put on the back of the polo ralph lauren, with such playres as Maldini, Toni, Di Rossi, Iaquinta, Pirlo, and Canavarro to name just a few cheappolos. And three are othre rising stars who might take your fancy burbrery polos, like Rossi, Balotelli, Pazzini and Aquilani. Ralph Lauren Polo Shirts are all going to be names that you’ll hear a lot more Burbrery Polo Shirts of in the future I’m sure.

tuo said...

ralph lauren polo shirts
chaussure puma
puma CAT

ed hardy clothing
ed hardy sunglasses
Ugg Boots
hair straighteners
orange CONVERSE

tuo said...

I like your blog . They are really great. Ermunterung ++ .
some new style Air shoes is in fashion this year.Do you know Air Shoes is a best . another kinds of nike air rift is better . the Puma basket will make you feel very relaxed when you play basketball.If you want to buy the Cheap puma shoes shoes ,you can buy them online. Very high-caliber and cheap puma shoes as same as you buy from the authorized store..My younger sister's long hair is always chaotic. so i presented a hair straighteners to her for Christmas last year .she were very pleasantly surprised .

lucyliu said...

nike air max 90
nike air max 95
nike air max tn
nike air rift
nike shox r4
nike air max 360
nike shox nz
puma mens shoes
puma shoes
puma speed
nike shoes
nike air
nike air shoes
puma cat
air max trainers
mens nike air max
nike shoes air max
nike shoes shox
air shoes
nike shoe cart
puma future
cheap puma
sports shoes
nike air rifts
nike air rift trainer
nike air
nike rift
nike rift shoes
cheap nike air rifts
bape shoes
jeans shop
diesel jeans
levis jeans

Sneakers hobbies said...

nice post!!
do you want keep yourself away from cold in this winter? Choose a good winter clothing is important things!welcome visit our online outlet,we're not only supply high quality but also most cheap winter jackets and ski jackets .