Take the 2-minute tour ×

In my web-site every pages can return html and json. If request is normal page returns html, if request is AJAX page returns json.

The problem is that firefox caches html response when I need json response. In both cases response headers with no-cache options

Connection  Keep-Alive
Date    Sat, 13 Apr 2013 08:31:06 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=5, max=100
Pragma  no-cache

This is how I am doing AJAX request:

    url: window.location.href,
    dataType: 'json',
    //cache: false,
    success: function(data) {
        // here I get html, (must be json)
        // If I set "cache: false" then all is ok

This problem is in firefox. In chrome all is ok

I think it because I'm sending the request on the page where I'm locating now. Because If I change url for example on window.location.href . '?a=1' and if I already been on page window.location.href . '?a=1' AJAX returns json how I want.

share|improve this question
That's quite odd behavior; no-cache does work in general in Firefox! Do you have a link to the page showing this behavior for you? –  Boris Zbarsky Apr 14 '13 at 3:09
I haven't got a link, it's on local machine yet. Its alright, no-cache works. But if the page has already opened in firefox (like in my case) and you are doing AJAX request on this page then response gets from cache. I agree with @allyourcode, its better to get JSON and HTML using different URLs. Now, I have /foo page and /foo.json for json –  Ildar Apr 14 '13 at 10:40
The point is what you describe is no-cache not working. Which is quite odd, since I just tried your example locally here and it works fine. Hence wanting to see a testcase where it's not working so I can debug it and fix whatever the problem is... –  Boris Zbarsky Apr 14 '13 at 14:42
add comment

2 Answers

up vote 1 down vote accepted

Why don't you change your URL scheme so that JSON and HTML are accessed using different URLs? E.g.

/foo.html vs. /foo.json


/foo?format=html vs. /foo?format=json

Don't think of it as a workaround for Firefox; you want to avoid decreasing cacheability as much as you can, because a highly cacheable site performs faster for your users, and reduces the amount of resources that you need to serve your site.

share|improve this answer
Good idea, thank you, maybe I will do it so –  Ildar Apr 13 '13 at 9:05
add comment

You can set the cache control to no cache on whatever file it is on charge of processing the ajax request


or try this:

if($_SERVER['HTTP_ORIGIN'] == "http://example.com")

    header('Access-Control-Allow-Origin: http://example.com');
    //your code here

   echo "<html>";
   echo "<head>";
   echo "   <title>Another Resource</title>";
   echo "</head>";
   echo "<body>",
share|improve this answer
it is already Cache-Control: no-cache, must-revalidate. But it does not help –  Ildar Apr 13 '13 at 8:45
so im guessing you are also setting the content type to return json: header('Content-type: application/json'); –  jycr753 Apr 13 '13 at 8:46
add comment

Your Answer


