在我的网站中,每个页面都可以返回 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/