Wednesday, January 16, 2008

IE caches your AJAX

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!

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:


<script>
var url = 'http://www.example.com/dir/foo?param1=a¶m2=b';
//add a unique param to the request (adjust to suit)
url += '&guid=' + new Date().getTime();
</script>





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


Example Workaround Code:


<!-- This example is using PHP -->
<?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");
?>

No comments: