I can reproduce this, but only if I do not include the X-Requested-With header in the ajax response. If I set the header for the ajax call, it works basically as expected, although the ajax call clears the cache for a regular request, and vice versa - the content is not cached, but you never get the wrong content.
My PHP document is as follows:
<? putenv('TZ=PST8PDT'); date_default_timezone_set('America/Los_Angeles'); header('Expires: '.gmdate("D, d MYH:i:s").' GMT'); header('Cache-Control: public, max-age=60'); header('Vary: X-Requested-With,Accept-Encoding'); echo 'it is now '.date('Ymd H:i:s'); ?>
And my test page is as follows:
<a href="resource.php" target="ifr">load into frame</a><br /> <iframe name="ifr" width="400" height="100"></iframe> <hr /> <a href="#" onclick="return load();">load into div via ajax</a><br /> <div id="di" style="border: 1px solid black; width: 400px; height: 100px;"></div> <script> function load(){ var req = new XMLHttpRequest(); req.onreadystatechange = function(){ if (req.readyState == 4){ document.getElementById('di').textContent = req.responseText; } } req.open('GET', 'resource.php', 1); req.setRequestHeader("X-Requested-With", "XMLHttpRequest"); req.send(null); return false; } </script>
When I click the first link, it requests from the server. When I hit it again, it comes from the cache. Each subsequent click comes from the cache, up to 60 seconds.
When I hit the second link, the request is sent to the server. When I hit it again, it comes from the cache. Each subsequent click comes from the cache, up to 60 seconds.
If I click link 1 and then link 2, they will both go to the server. If I delete link 1 again, it will reappear on the server (which is wrong). Demo sequence (provided that everything is within 60 seconds):
Reg : server Reg : cache Reg : cache Reg : cache Ajax : server Ajax : cache Reg : server Ajax : server
The result is that if you want to cache things differently reliably when served through ajax, use a different url when executing the ajax request (? Ajax = 1 will work fine).
I am testing the latest version of FF 4.0