javascript - 当我使用 AJAX 时,Firefox 会忽略 Cache-Control no-store, no-cache

标签 javascript firefox browser-cache

在我的网站中,每个页面都可以返回 html 和 json。 如果请求是普通页面返回html,如果请求是AJAX页面返回json。

问题是当我需要 json 响应时,firefox 会缓存 html 响应。 在这两种情况下,响应 header 都带有无缓存选项

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
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

这就是我执行 AJAX 请求的方式:

$.ajax({
    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
    }
});

此问题出现在 Firefox 中。在 Chrome 中一切正常

我认为这是因为我正在我现在所在的页面上发送请求。因为如果我更改 url 例如 window.location.href 。 '?a=1' 如果我已经在 window.location.href 页面上。 '?a=1' AJAX 按照我想要的方式返回 json。

最佳答案

为什么不更改 URL 方案,以便使用不同的 URL 访问 JSON 和 HTML?例如

/foo.html 与/foo.json

/foo?format=html 与/foo?format=json

不要将其视为 Firefox 的解决方法;您希望尽可能避免降低缓存能力,因为高度可缓存的网站对用户来说执行速度更快,并且减少了为网站提供服务所需的资源量。

关于javascript - 当我使用 AJAX 时,Firefox 会忽略 Cache-Control no-store, no-cache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15985907/

相关文章:

c# - 需要 google chrome 或 firefox 自动化 c#

c++ - 如何阻止 Curl 缓存数据

javascript - 无法运行 angularjs 文件 Controller 错误

javascript - WebStorm 尝试打开 Chrome 进行调试

javascript - 如何在 Angular 2+ 中获取 json 数据

javascript - Flash v11.8.800.168 函数调用在 Internet Explorer 中失败

css - em 在 Firefox 和 Chrome 中以及在 Windows 和 Linux 中具有不同的 px 大小

javascript - 单击时需要向下滑动巨型菜单

http - IE 缓存行为 : response 304 followed by 200's on same resource?

browser-cache - Service Worker 响应缓存 header