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()
?
What about using a POST request instead of a GET...? (Which you should anyway...)
As @Athasach said, according to the jQuery docs,
$.ajaxSetup({cache:false})
will not work for other than GET and HEAD requests.You are better off sending back a
Cache-Control: no-cache
header from your server anyway. It provides a cleaner separation of concerns.Of course, this would not work for service urls that you do not belong to your project. In that case, you might consider proxying the third party service from server code rather than calling it from client code.
I use
new Date().getTime()
, which will avoid collisions unless you have multiple requests happening within the same millisecond: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.
JQuery's $.get() will cache the results. Instead of
you should use $.ajax, which will allow you to turn caching off:
Maybe you should look at $.ajax() instead (if you are using jQuery, which it looks like). Take a look at: http://docs.jquery.com/Ajax/jQuery.ajax#options and the option "cache".
Another approach would be to look at how you cache things on the server side.
All the answers here leave a footprint on the requested URL which will show up in the access logs of server.
I needed a header based solution with no side effect and I found it can be achieved by setting up the headers mentioned in How to control web page caching, across all browsers?.
The results, working for Chrome at least, would be