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

コールバックを追加します。それは の 3 番目のパラメータになります$.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 に答える