Nik
Nik

Reputation: 7273

Disable AJAX Caching

I am in a bit of a pickle right now. I am building a web page that will get data from a CGI backend. I have no control over the CGI backend, nor the server (so no mod_headers or mod_expires). Also, because of the parameters to the script, I cannot append a unique value (like '&089u0af0d98) to each request. The requests are AJAX using the XmlHttpRequest object. I have tried to set the 'If-Modified-Since' and 'Cache-Control' request headers unsuccessfully. Does anybody have any other ideas for how I can prevent the AJAX response from being cached by the browser?

Upvotes: 11

Views: 53423

Answers (5)

Makassi
Makassi

Reputation: 71

I used $.ajaxSetup({ cache: false }); somewhere in my base html-page (default.aspx) in a non-frequent web-system and it worked fine. No pain-in-the-neck caching problems anymore.

Upvotes: 7

Bart B
Bart B

Reputation: 669

I ran into this today, and found that if you want to keep to using get, you can add a hidden form element to the page and have JS set it's value to the current timestamp before submitting the query to ajax.

I add a form element something like this:

<input type="hidden" name="ie_sucks" id="ie_sucks", value="1" />

Then, in the function to submit the form via AJAX I set this hidden input to the current timestamp with something like this:

$('#ie_sucks').val(new Date().getTime());

The above code uses JQuery, so in pure JS it would be something like:

document.getElementById('ie_sucks').value = new Date().getTime();

This is not a pretty solution, but it does work.

Upvotes: 4

Vikas
Vikas

Reputation: 1101

I use this javascript function ( which in turn uses jquery.ajax function ) the cache: false would do the trick. This works perfectly for me , may be you can give it a try

    function ajax_call(urlString)
    {
        ret_val="";
        $.ajax
        (
            {
                type: "GET",
                url: urlString,
                async:false,
                cache:false,
                dataType: 'html',
                success: function(msg)
                {
                    ret_val=msg;
                },
                error:function (xhr, textStatus, thrownError)
                {
                    ret_val=xhr.readyState;
                    alert("status=" +xhr.status);
                }
            }
        );
        return ret_val;
    } 

Upvotes: 13

Marc
Marc

Reputation: 1049

You can send random parameters using POST, while sending the important vars using GET if you need to.

If you have problems with IE, I know that sending something with POST makes it to stop caching server responses

Upvotes: 14

David
David

Reputation: 218798

I know jQuery's .ajax() call has a parameter called 'cache' which, if set to false, will force requested pages not to be cached by the browser. It's probably worth checking the jQuery source to see how they do it.

(I'm checking it now and will update if I find anything, but posting this answer early in case you or anybody else has better luck finding it.)

Upvotes: 1

Related Questions