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

查看:25
本文介绍了防止浏览器缓存 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 答案也很有用.

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天全站免登陆