jQuery 强制ajax调用清除缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4060287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:35:49  来源:igfitidea点击:

Force ajax call to clear cache

jqueryajaxcachingxmlhttprequestclear-cache

提问by sasklacz

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 $.ajaxbut it's not working.

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

Here's a sample page :

这是一个示例页面:

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

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

And my js :

而我的 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;
});

采纳答案by Ken Redler

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-listtable with some html that includes the <table>element itself. So you'd have <table...><table...>, etc., after the .html()content replacement.

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

回答by lonesomeday

What cache: falsedoes 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:

什么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]
    }
});

回答by remi

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.

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