问题描述
如果我使用 $。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);
});
编辑:这个答案已有好几年了。它仍然有效(因此我没有删除它),但是现在有更好/更干净的方法来实现这一目标。我的偏好是方法,但是
如果您想在页面的生命周期内禁用每个请求的缓存,此答案也很有用。
这篇关于防止浏览器缓存AJAX调用结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!