如何防止 Ajax 缓存

发布于 2024-12-12 19:45:01 字数 1168 浏览 2 评论 0原文

我创建这个类是为了从 Web 获取文件以使用 Ajax 检查新版本。 该代码在 Windows 设备上运行,即 IE8。但由于缓存,我遇到了麻烦。 有没有办法修复这个 Ajax 类以禁用缓存?

PS:我不使用任何库或框架。

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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2024-12-19 19:45:01

您可以将当前时间戳作为 url 中的变量传递,如下所示:

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

此外,您可以使用正确的标头强制在服务器端重新加载页面

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文