Page 1 of 1

Fixing the jQuery AJAX Page Caching Problem

Posted: Tue Nov 25, 2008 9:17 pm
by volomike
You may experience in certain browsers, or certain browser versions even, where you use jQuery's .load() event or other AJAX call to pull back a page from a server and load it into a DIV or something, and the page is cached instead of getting the latest copy. Of course, unfortunately headers are already sent when you do that, so you can't turn fix the page cache through a header() call in the PHP of that page. Turns out, this is a common problem. The fix is this:

Code: Select all

$().ready(function(){ //when page has fully loaded
  $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
  });
});
 
You'd think that would be off by default with jQuery, but not so!

Re: Fixing the jQuery AJAX Page Caching Problem

Posted: Thu Nov 27, 2008 12:37 pm
by kaszu
What 'cache' option does is adds timestamp to the url query, making browser think it's a different url.
You can fix it with headers by setting them for ajax response, not the document itself.