3

uploadedImagesRefresh()当 POST 请求成功时,我试图调用一个方法。该代码有时有效,但有时无法调用,uploadedImagesRefresh()但我必须说后端没问题,并且 Post 请求成功完成。

function refreshUploadedImages() {//this
    //function works quite well in other calls
}

$(function(){
    //
    $('.delete-img-btn').live('click', function() {

        //asign the image id from the button attribute 'img-id'
        var id= $(this).attr('img-id');
        //The data to be send via ajax the server will recieve 2 POST variables ie. 'action' and 'id'(which is the img id)
        var data={
            'pk':id,
            //call refreshUploadImages() upon succesful post
            'success':refreshUploadedImages
        };
        //The ajax request.

        $.post("/admin/image-uploader/delete/"+id , data);

    });
});

我在上面的代码中做错了什么?

4

1 回答 1

5

添加回调。那将是$.post(). 当 AJAX POST 请求成功时,将调用该回调函数。在该函数中,您可以调用您的函数。

$.post("/admin/image-uploader/delete/"+id , data,function(data){
  refreshUploadedImages()
});

您甚至可以直截了当,将您的函数设为回调:

$.post("/admin/image-uploader/delete/"+id , data, refreshUploadedImages);
于 2013-03-05T03:27:19.077 回答