Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax cache disabling not working?

I'm developing a Firefox extension that uses an ajax-request to retrieve information. This is the code:

$.ajax({
        url: "http://127.0.0.1/foo/bar/Service?wsdl" + new Date().getTime(),
        beforeSend: function(request) { request.setRequestHeader("SOAPAction", "Group"); },         
        async: false,
        cache: false,
        type: "POST",   
        dataType: "xml",
        data: req,
        contentType: "text/xml; charset=\"utf-8\"",
        success: function (data, textStatus, xmlHttpRequest) {
        out = $(xmlHttpRequest.responseXML);
        }
    }); 

I still get the same result, when the data that is sent to the server is changed. I tried to avoid that by adding "new Date().getTime()" to the URL and "cache: false". This doesn't seem to work. After restarting the browser, I get the correct results.

Does anyone have an idea what the problem is? Is there some kind of session-handling, so the server still gives back the old response?

Edit: I did a lot of testing and debugging and I think I found the problem: there is a cookie saved with every ajax-request that contains a session-id, so every time I do the request again, the server sends data of the session with the session-id in the cookie. Really bad behavior, I didn't know that cookies could be created through an ajax-request. So everything I have to do to fix the problem is a function that deletes this cookie every time my parameters are changed. Thanks for your help again.

like image 276
Bob Avatar asked Dec 04 '10 22:12

Bob


People also ask

Are Ajax calls cached?

Although we can use a standard caching solution provided by HTTP (yes, Ajax is cached by HTTP), there is a catch: It works for GET requests only (not POST).

Does browser cache AJAX requests?

Fact #1 : Ajax Caching Is The Same As HTTP Caching At this level, the browser doesn't know or care about Ajax requests. It simply obeys the normal HTTP caching rules based on the response headers returned from the server. If you know about HTTP caching already, you can apply that knowledge to Ajax caching.

What is jqXHR in Ajax?

The jqXHR Object. The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.


2 Answers

Can you try this

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

try toger with Math.random() to be on safer side

Date().getTime() together with Math.random()

http://127.0.0.1/foo/bar/Service?wsdl" + new Date().getTime() + Math.random()
like image 114
kobe Avatar answered Oct 25 '22 18:10

kobe


Try an ampersand before the timestamp:

http://127.0.0.1/foo/bar/Service?wsdl&" + new Date().getTime()

I guess the wsdl-parameter makes sense there inside the url, without the ampersand you destroy the parameter.

like image 22
Dr.Molle Avatar answered Oct 25 '22 18:10

Dr.Molle