.done和.fail都会引发攻击

时间:2017-01-04 12:13:23

标签: javascript jquery ajax

我有这个JS:

$('.save').click(function(e){
            var row = $(this).closest('tr');
            var button = $(this);
            var myParams = new Object();
            myParams.id = row.find('.id').text();
            myParams.data = row.find('.data').text();
            myParams.descrizione = row.find('.descrizione').text();
            myParams.movimento = row.find("select[name='tipo_mov']").val();
            myParams.importo = row.find('.importo').text();
            myParams.from = 'carta';
            var params = JSON.stringify(myParams);
            $.post( "edit_mov.php", params)
             .done(function( data ) {
                bootbox.alert("Movimento aggiornato correttamente");
                button.toggle().prev('.edit').toggle();//ripristino il pulsante di edit;
                row.removeClass('info').addClass('success').find('.tbe').attr('contenteditable', false).css('color','');
                row.find('.tipo_mov').toggle();
                setTimeout(function () { 
                    row.removeClass('success');
                }, 2000);
            })
             .fail(bootbox.alert("UPS! Something went wrong"));

        });

这样做是为了使用AJAX请求更新表行。 负责更新的PHP页面将返回200或500,具体取决于查询是否成功:

if($count==0){
    http_response_code(500);
}else{
    http_response_code(200);
}

如果我尝试查询失败,我的JS将仅显示.fail中的警报。 如果我尝试查询将成功,那么我将看到两个警报(.done和.fail)。

我也尝试用.success buth取代.done,结果相同。我做错了什么?

2 个答案:

答案 0 :(得分:3)

您还应该使用.fail中的函数:

.fail(function() {
    bootbox.alert("UPS! Something went wrong");
});

否则,括号内的代码总是被执行。

答案 1 :(得分:0)

根据文档。

https://api.jquery.com/jquery.post/

Contact

.fail应该提供一个函数

相关问题