prevent xmlHttpReq 's browser caching

孤街浪徒 提交于 2019-12-21 09:02:43

问题


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?


回答1:


For every AJAX request you make, generate a unique value and add it to the ajax URL as a query parameter:

/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.

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.

If you were to use jQuery, it does this for you by setting the property cache to false on the AJAX settings.



来源:https://stackoverflow.com/questions/3984961/prevent-xmlhttpreq-s-browser-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!