Tuesday, 20 July 2010

AJAX jQuery GET caching

It seems the HTTP specification allows anything in a GET request chain to cache the results of a request.  Internet Explorer has apparently been well known for this but now Firefox has started to exhibit the same behaviour.
If using jQuery ajax GET requests you need to do this to prevent it:
    $.ajaxSetup({cache: false});
In addition, to ensure the results are definitely not cached a random string can be added to the end of the request URL e.g.
    var randomNumber = Math.floor(Math.random() * 1000);
    var url = targetUrl + '?rnd=' + randomNumber;