Prevent browser caching of AJAX call result

后端 未结 21 1203
醉酒成梦
醉酒成梦 2020-11-22 04:47

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 iss

相关标签:
21条回答
  • 2020-11-22 05:03

    The real question is why you need this to not be cached. If it should not be cached because it changes all the time, the server should specify to not cache the resource. If it just changes sometimes (because one of the resources it depends on can change), and if the client code has a way of knowing about it, it can append a dummy parameter to the url that is computed from some hash or last modified date of those resources (that's what we do in Microsoft Ajax script resources so they can be cached forever but new versions can still be served as they appear). If the client can't know of changes, the correct way should be for the server to handle HEAD requests properly and tell the client whether to use the cached version or not. Seems to me like appending a random parameter or telling from the client to never cache is wrong because cacheability is a property of the server resource, and so should be decided server-side. Another question to ask oneself is should this resource really be served through GET or should it go through POST? That is a question of semantics, but it also has security implications (there are attacks that work only if the server allows for GET). POST will not get cached.

    0 讨论(0)
  • 2020-11-22 05:03

    Internet Explorer’s Ajax Caching: What Are YOU Going To Do About It? suggests three approaches:

    1. Add a cache busting token to the query string, like ?date=[timestamp]. In jQuery and YUI you can tell them to do this automatically.
    2. Use POST instead of a GET
    3. Send a HTTP response header that specifically forbids browsers to cache it
    0 讨论(0)
  • 2020-11-22 05:04

    Now, it's easy to do it by enabling/disabling cache option in your ajax request, just like this

    $(function () {
        var url = 'your url goes here';
        $('#ajaxButton').click(function (e) {
            $.ajax({
                url: url,
                data: {
                    test: 'value'
                },
                    cache: true, //cache enabled, false to reverse
                    complete: doSomething
                });
            });
        });
        //ToDo after ajax call finishes
        function doSomething(data) {
            console.log(data);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 05:06

    another way is to provide no cache headers from serverside in the code that generates the response to ajax call:

    response.setHeader( "Pragma", "no-cache" );
    response.setHeader( "Cache-Control", "no-cache" );
    response.setDateHeader( "Expires", 0 );
    
    0 讨论(0)
  • 2020-11-22 05:06

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

    0 讨论(0)
  • 2020-11-22 05:08

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

    0 讨论(0)
提交回复
热议问题