• Last Modified
  • 12.04.2007

Ajax + IE Caching Problem

Problem Description: Using XML HTTP Request, Internet Explorer caches response sent back from the server script.

Solution: You have to send several headers along with response to the browser to prevent caching.

Script has been tested in IE/Firefox and works fine...


Turn Caching "OFF"

<? 
//PHP Requests Handler

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
header ("Pragma: no-cache");
//header ("Content-type: application/xml"); //uncomment this line if you plan to send XML

//...................
echo "you should see now random numbers in your browser:".rand().", timestamp: ".time();	

?>


Turn Caching "On"

You may have script on the server, that takes time to execute, like displaying sitemaps or customer database.

So, you don't want to call the script again and again, but just tell the browser to get results from the cache.

All you have to do, is to skip execution of the script on the server and send empty response with the headers below!

<? 
//PHP Requests Handler

header("Expires: Mon, 26 Jul 3000 05:00:00 GMT");    // Date in the future
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: cache"); 
header("Pragma: cache");
//header ("Content-type: application/xml"); //uncomment this line if you plan to send XML


//...................
echo "you should always see the same result in your browser:".rand().", timestamp: ".time();		

?>
  • Bookmark this page on Delicious
  • Bookmark this page on StumbleUpon
  • Digg this page on Digg
  • Submit this page on Reddit
  • Submit this page on Furl
  • Share this page on Facebook
  • Bookmark this page on Google
  • Bookmark this page on Yahoo
  • Search for links to this page on Technorati
  • Search for links to this page on IceRocket

Comments

2007-04-04
Stanislav Muller
there is another 2 simple solutions to prevent caching ajax requests 1. you add timestamp on the end of your url like: ?12345555532 or 2. you send a post request (post request are not cached)


Leave a comment (E-mail address never displayed)





2006-2007 @ SkyByte.net. All Rights Reserved xhtml 1.0 css