Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable AJAX Caching

Tags:

ajax

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?

like image 613
Nik Avatar asked Jun 02 '11 14:06

Nik


2 Answers

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

like image 114
Marc Avatar answered Oct 14 '22 15:10

Marc


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;
    } 
like image 38
Vikas Avatar answered Oct 14 '22 14:10

Vikas