Fri 09 Nov 2007

Caching pages leads to a speedier browsing experience and so is a good idea. Replacing information within a web page using Ajax similarly improves the user’s experience. The problem comes when the browser caches the information from the Ajax request. It’s a logical thing to do, but it’s not normally what the developer wants.

There are a number of ways round this. One way is to add an extra parameter to the request with the current time in milliseconds or something similar that will make each request unique. This works if you can control the page making the request, but can’t modify the source of the Ajax data.

If you can modify the code that generates the data for the Ajax request, then you can use the same mechanisms as you use to control normal page caching. For example in a JSP you can use:

<%
// To ensure that IE doesn't cache the results of an Ajax call
response.setHeader("Cache-Control","no-store, no-cache, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>

This works for IE6 and IE7. Firefox and Safari don’t seem to cache Ajax requests.



x
This website uses cookies. More info. That's OK