Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force ajax call to clear cache

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;
});
like image 933
sasklacz Avatar asked Oct 30 '10 19:10

sasklacz


People also ask

Do AJAX calls get cached?

Although we can use a standard caching solution provided by HTTP (yes, Ajax is cached by HTTP), there is a catch: It works for GET requests only (not POST).

How do I set cache false in AJAX call?

The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use. We can use $. ajaxSetup({cache:false}); to apply the technique for all AJAX functions.

What is processData in AJAX?

processData. If set to false it stops jQuery processing any of the data. In other words if processData is false jQuery simply sends whatever you specify as data in an Ajax request without any attempt to modify it by encoding as a query string.


1 Answers

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]
    }
});
like image 53
lonesomeday Avatar answered Nov 05 '22 15:11

lonesomeday