繁体   English   中英

在jquery ajax发布成功后调用方法

[英]call a method after success post jquery ajax

我正在尝试在POST请求成功时调用方法uploadedImagesRefresh() 代码有时工作,但有时它无法调用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);

    });
});

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

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

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

你甚至可以直截了当,而是让你的函数成为回调:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM