Skip to main content

Prevent caching of AJAX call



It looks like that if I load dynamic content using $.get(), the result is cached in browser. Adding some random string in QueryString seem 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() ?



Source: Tips4all

Comments

  1. I use new Date().getTime(), which will avoid collisions unless you have multiple requests happening within the same millisecond.

    ReplyDelete
  2. The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)

    $(document).ready(function() {
    $.ajaxSetup({ cache: false });
    });

    ReplyDelete
  3. Personally I feel that the query string method is more reliable than trying to set headers on the server - there's no guarantee that a proxy or browser won't just cache it anyway (some browsers are worse than others - naming no names).

    I usually use Math.random() but I don't see anything wrong with using the date (you shouldn't be doing AJAX requests fast enough to get the same value twice).

    ReplyDelete
  4. A small addition to the excellent answers given: If you're running with a non-ajax backup solution for users without javascript, you will have to get those server-side headers correct anyway. This is not impossible, although I understand those that give it up ;)

    I'm sure there's another question on SO that will give you the full set of headers that are appropriate. I am not entirely conviced miceus reply covers all the bases 100%.

    ReplyDelete
  5. What about using a POST request instead of a GET...?
    (Which you should anyway...)

    ReplyDelete
  6. 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.

    ReplyDelete
  7. Of course "cache-breaking" techniques will get the job done, but this would not happen in the first place if the server indicated to the client that the response should not be cached. In some cases it is beneficial to cache responses, some times not. Let the server decide the correct lifetime of the data. You may want to change it later. Much easier to do from the server than from many different places in your UI code.

    Of course this doesn't help if you have no control over the server.

    ReplyDelete
  8. You can use the short notation $.now() instead of doing a (new Date().getTime()) each and everytime.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex