Site Navigation

Friday, November 9, 2007

bug 146 - IE caches your AJAX

Issue: #146
Affects: IE6, IE7

So, you're doing all kinds of funky stuff with AJAX now, and you've come to notice in your testing that sometimes Internet Explorer is not really hitting your server, but instead caching your AJAX calls?! Even if you set the no-cache header!

As Eric Lawrence points out over on his site Enhance IE, this bug (#IE0001) is only a "bug" when IE caches and a query string is present; The HTTP standard does not prohibit caching if a query-less GET returns no cache indicators.

Known Workarounds: Two. If you are calling a particular request repetitively, but are expecting different results, add a unique parameter to the request.

Example Workaround Code:

var url = '¶m2=b';
//add a unique param to the request (adjust to suit)
url += '&guid=' + new Date().getTime();

Known Workaround: Second Option.
Set the full cache header information, not just the no-cache.

Example Workaround Code:

<!-- This example is using PHP -->
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: no-store, no-cache, must-revalidate");

Related Issues: None.