狐狸.fox 2008-12-15 08:44 采纳率: 0%
浏览 233
已采纳

防止浏览器缓存 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条回答 默认 最新

  • from.. 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 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发