强制ajax调用清除缓存

owc*_*wca 5 ajax jquery caching xmlhttprequest clear-cache

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

这是一个示例页面:

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;
});
Run Code Online (Sandbox Code Playgroud)

lon*_*day 14

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]
    }
});
Run Code Online (Sandbox Code Playgroud)


rem*_*emi 7

只需更换

url: "/manage/update_position/",
Run Code Online (Sandbox Code Playgroud)

url: "/manage/update_position/?nocache="+Math.random(),
Run Code Online (Sandbox Code Playgroud)

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


Ken*_*ler 2

你有

$("objects-list").html(data["html"]);
Run Code Online (Sandbox Code Playgroud)

试试这个:

$(".objects-list").html(data["html"]); // forgot leading dot?
Run Code Online (Sandbox Code Playgroud)

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