Вопрос

I'm created this class to fetch a file from web to check for new version using Ajax. This code run on a Windows gadget, on IE8. But I'm having trouble because of the cache. Is there a way to fix this Ajax class to disable cache?

PS: I don't use any library or frameworks.

var ClassAjax = function() {

    this.data = null;

    var that = this;

    this.get = function(url, send) {

        var ajax = new function ObjAjax() {
            try{ return new XMLHttpRequest(); }
            catch(e){try{ return new ActiveXObject("Msxml2.XMLHTTP"); }
            catch(e){ return new ActiveXObject("Microsoft.XMLHTTP"); }}
            return null;
        }

        ajax.onreadystatechange = function() {
            if(ajax.readyState == 1) { that.onLoading(); }
            if(ajax.readyState == 4) { that.data=ajax.responseText; that.onCompleted(that.data); }  
        }
        ajax.open("GET", url, true);
        ajax.send(send);

    };

    this.onLoading = function() {
        //function called when connection was opened
    };

    this.onCompleted = function(data) {
        //function called when download was completed
    };
}

var request = new ClassAjax();
request.onCompleted = function(data) { alert(data); }
request.get('http://exemple.com/lastversion.html', null);
Это было полезно?

Решение

You can pass the current timestamp as a variable in the url, like this :

var timestamp = new Date().getTime();
ajax.open("GET", url+'?ts='+timestamp, true);

Also, you can force the page to be reloaded on server-side, using the proper headers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top