Javascript .done和.fail同时触发
我有这个JS:Javascript .done和.fail同时触发,javascript,jquery,ajax,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('.dat
$('.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中的警报。
如果我尝试一个将成功的查询,那么我将同时看到alerts.done和.fail
我也试着用同样的结果替换.done with.success。我做错了什么?您还应该在中使用函数。失败: 否则,括号内的代码将始终执行。根据文档
.fail应提供一个函数可能与的重复项,然后您还应否决jQuery文档页面。这是一个关于“完成”、“失败”和“始终回调”的结构应该是什么的示例。我不知道为什么人们在没有对答案进行适当思考的情况下否决了投票
.fail(function() {
bootbox.alert("UPS! Something went wrong");
});
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});