防止浏览器缓存AJAX调用结果 [英] Prevent browser caching of AJAX call result

查看:88
本文介绍了防止浏览器缓存AJAX调用结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 $。get()加载动态内容,结果将存储在浏览器中。

It looks like if I load dynamic content using $.get(), the result is cached in browser.

在QueryString中添加一些随机字符串似乎可以解决此问题(我使用 new Date()。toString()),但这感觉像是一种破解。

Adding some random string in QueryString seems to solve this issue (I use new Date().toString()), but this feels like a hack.

还有其他方法可以实现吗?
或者,如果唯一字符串是实现此目的的唯一方法,则除了 new Date()之外的其他建议?

Is there any other way to achieve this? Or, if unique string is the only way to achieve this, any suggestions other than new Date()?

推荐答案

我使用 new Date()。getTime(),这将避免冲突,除非您在同一毫秒:

I use new Date().getTime(), which will avoid collisions unless you have multiple requests happening within the same millisecond:

$.get('/getdata?_=' + new Date().getTime(), function(data) {
    console.log(data); 
});

编辑:这个答案已有好几年了。它仍然有效(因此我没有删除它),但是现在有更好/更干净的方法来实现这一目标。我的偏好是方法,但是
如果您想在页面的生命周期内禁用每个请求的缓存,此答案也很有用。

This answer is several years old. It still works (hence I haven't deleted it), but there are better/cleaner ways of achieving this now. My preference is for this method, but this answer is also useful if you want to disable caching for every request during the lifetime of a page.

这篇关于防止浏览器缓存AJAX调用结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆