derek5. 2008-12-15 08:44 采纳率: 100%
浏览 229
已采纳

防止浏览器缓存 jQuery AJAX 调用结果

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

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

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()?

转载于:https://stackoverflow.com/questions/367786/prevent-browser-caching-of-jquery-ajax-call-result

  • 写回答

20条回答 默认 最新

  • python小菜 2008-12-15 09:12
    关注

    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); 
    });
    

    Edit: 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(19条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突