javascript 防止 xmlHttpReq 的浏览器缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3984961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
prevent xmlHttpReq 's browser caching
提问by Girish
It seems that all the browsers other than Opera cache XHR requests in my Javascript program. The URL in the XHR request refers to a CGI program on the server which generates a different output everytime its called. B Is there a way in Javascript to make the browser not cache XHR requests?
似乎除 Opera 之外的所有浏览器都在我的 Javascript 程序中缓存了 XHR 请求。XHR 请求中的 URL 指的是服务器上的 CGI 程序,该程序每次调用都会生成不同的输出。B Javascript 中有没有办法让浏览器不缓存 XHR 请求?
回答by Glycerine
For every AJAX request you make, generate a unique value and add it to the ajax URL as a query parameter:
对于您发出的每个 AJAX 请求,生成一个唯一值并将其作为查询参数添加到 ajax URL:
/example/url/post.php?rand=09809807896768
I use to generate the current unix timestamp in JS and use that - this ensures I do not get duplicated stamps.
我用来在 JS 中生成当前的 unix 时间戳并使用它 - 这确保我不会得到重复的邮票。
That way every request is unique and will not get cached. This is a simple but fairly common AJAX trick - usually fixing IE and testing issues.
这样每个请求都是唯一的,不会被缓存。这是一个简单但相当常见的 AJAX 技巧——通常用于修复 IE 和测试问题。
If you were to use jQuery, it does this for you by setting the property cache to false on the AJAX settings.
如果您要使用 jQuery,它会通过在AJAX 设置上将属性缓存设置为 false 来为您完成此操作。

