Tuesday, November 06, 2007

IE 6 caching my Ajax RPCs

I am writing some Ajax (GET) calls and Firefox is fine - each call returns the expected result. But under IE 6 it is returning cached results.

Turn off cache

I can turn caching off and this fixes it. Tools | Internet Options | General tab | Temporary Internet Files: click Settings | "Check for newer versions of stored pages": click "Every visit to the page".

But that's not nice - I don't want to turn caching off for all of my pages, and I don't want to ask users to turn their cache off either.


Yet if you read the HTTP specification, you'll learn that any resource got using GET method is due to be cached by any agent on the chain (browser, proxy, even web server); whereas if got using the POST method, the query will be resubmitted and reprocessed each and every time. - Ajax IE Caching Issue

Make your URLs unique

Make Your Remote Procedure Calls Have an Unprecedented URL  - from HowToAdvice.com.

If you have to use GET, add a new parameter to your URL to make it unique each time:

var url = "http://www.yourdomain.com/?id=47" + "&ms=" + new Date().getTime();

Personally, I changed to POST.

BTW - I found this problem while going through the examples in Head Rush Ajax, a very cool way to intro to this subject. After making this post, I flipped the page to page 59 and found they knew about this issue and deal with in Chapter 2. The say to add the dummy new Date().getTime() parameter. :)


Anonymous said...

Thank you for that, IE is lame.

Anonymous said...

Thanks man! Saved me alot of time!!! Oh and IE does suck.