强制ajax调用清除缓存

发布于 2024-09-29 17:46:10 字数 1541 浏览 5 评论 0原文

我有一个 cms,可以在其中更改对象的位置。每次位置更改后,ajax 调用都会更新整个对象列表。但不幸的是,一些数据存储在缓存中,并且没有可见的更改。有没有办法用 javascript/request/other 强制清除缓存?我已经在 $.ajax 中尝试过“cache: false”,但它不起作用。

这是一个示例页面:

http://ntt.vipserv.org/manage/playforward

和我的js :

$(".object-position").livequery("change", function() {
    $("#objects-list input").attr('disabled', true);
    var action = $(this).attr('name');
    var position = $(this).attr('value');
    var id = $(this).attr("id");
    var model = id.split("-")[0];
    var object_id = id.split("-")[1];

    $("#loader").show();
    $("#loader").fadeIn(200);

    $.ajax({
        type: "POST",
        async: true,
        url: "/manage/update_position/",
        data: "action=" + action + "&model=" + model + "&object_id=" + object_id + "&position=" + position,
        dataType: "json",
        success: function(data){
            $("#loader").fadeOut("fast", function () {
                $("#loader").hide();
            });
            $("objects-list").html(data["html"]);
            $("#message").show();
            $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
            setTimeout(function(){
                $("#message").fadeOut("slow", function () {
                    $("#message").hide();
                });
            }, 1500); 
        }
    });
    $("#objects-list input").attr("disabled", false);
    return false;
});

I have a cms in which I can change positions of objects. After each position change ajax call updates the whole list of objects. But unfortunately some data is stored in cache and no changes are visible. Is there a way to force clearing cache with javascript/request/other ? I've tried 'cache: false' in $.ajax but it's not working.

Here's a sample page :

http://ntt.vipserv.org/manage/playforward

And my js :

$(".object-position").livequery("change", function() {
    $("#objects-list input").attr('disabled', true);
    var action = $(this).attr('name');
    var position = $(this).attr('value');
    var id = $(this).attr("id");
    var model = id.split("-")[0];
    var object_id = id.split("-")[1];

    $("#loader").show();
    $("#loader").fadeIn(200);

    $.ajax({
        type: "POST",
        async: true,
        url: "/manage/update_position/",
        data: "action=" + action + "&model=" + model + "&object_id=" + object_id + "&position=" + position,
        dataType: "json",
        success: function(data){
            $("#loader").fadeOut("fast", function () {
                $("#loader").hide();
            });
            $("objects-list").html(data["html"]);
            $("#message").show();
            $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
            setTimeout(function(){
                $("#message").fadeOut("slow", function () {
                    $("#message").hide();
                });
            }, 1500); 
        }
    });
    $("#objects-list input").attr("disabled", false);
    return false;
});

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

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

发布评论

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

评论(3

碍人泪离人颜 2024-10-06 17:46:10

cache: false 的作用是将时间添加到请求数据中,因此每个请求实际上都是唯一的,因此绕过了浏览器的缓存。我想知道您使用数据字符串而不是对象的事实是否导致了这里的问题。尝试使用一个对象来代替:

$.ajax({
    type: "POST",
    async: true,
    url: "/manage/update_position/",
    data: {
        "action": action.
        "model": model,
        "object_id": object_id,
        "position": position
    },
    cache: false,
    dataType: "json",
    success: function(data){
        //[snip]
    }
});

What cache: false does is to add the time to the request data, so each request is effectively unique and therefore bypasses the browser's cache. I wonder if the fact that you are using a data string rather than an object is causing problems here. Try using an object instead:

$.ajax({
    type: "POST",
    async: true,
    url: "/manage/update_position/",
    data: {
        "action": action.
        "model": model,
        "object_id": object_id,
        "position": position
    },
    cache: false,
    dataType: "json",
    success: function(data){
        //[snip]
    }
});
桃扇骨 2024-10-06 17:46:10

只需替换

url: "/manage/update_position/",

url: "/manage/update_position/?nocache="+Math.random(),

即可强制重新加载页面,而不使用浏览器的缓存。

Just replace

url: "/manage/update_position/",

with

url: "/manage/update_position/?nocache="+Math.random(),

to force reloading the page not using the browser’s cache.

や三分注定 2024-10-06 17:46:10

您可以

$("objects-list").html(data["html"]);

尝试以下操作:

$(".objects-list").html(data["html"]); // forgot leading dot?

此外,您似乎正在尝试用一些包含 的 html 替换 .objects-list 表的内容元素本身。因此,在 .html() 内容替换之后,您将得到 等。

You have

$("objects-list").html(data["html"]);

Try this instead:

$(".objects-list").html(data["html"]); // forgot leading dot?

Also, it looks like you're trying to replace the contents of the .objects-list table with some html that includes the <table> element itself. So you'd have <table...><table...>, etc., after the .html() content replacement.

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