-1

I have this 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"));

        });

This is done to update a table row with an AJAX request. The PHP page responsible for update will return 200 or 500 depending if the query is successful or not:

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

If I try with a query that will fail my JS will show only the alert in the .fail. If I try with a query that will succeed then I will see both the alerts (.done and .fail).

I also tried to replace .done with .success buth with the same results. What am I doing wrong?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • Possible duplicate of [Javascript callback function with parameters](http://stackoverflow.com/questions/13003828/javascript-callback-function-with-parameters) – JJJ Jan 04 '17 at 12:17

2 Answers2

3

You should also use a function in .fail:

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

Otherwise the code inside the brackets is always executed.

Peter B
  • 22,460
  • 5
  • 32
  • 69
0

as per docs.

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

var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

.fail should be supplied with a function

Deep
  • 9,594
  • 2
  • 21
  • 33
  • 1
    Then you should also vote down the jQuery docs page. It is an EXAMPLE of what you can do. – Peter B Jan 04 '17 at 12:25
  • this was an example about what the structure should be for for done , fail and alway callback. I do not know why people down vote without giving proper thought about the answer – Deep Jan 04 '17 at 12:27