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

查看:184
本文介绍了防止浏览器缓存jQuery 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.

还有其他方法可以达到这个目的吗?
或者,如果唯一字符串是实现此目的的唯一方法,除之外的任何建议新的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.

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

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