Joe
Joe

Reputation: 546

Ajax success if statement

When the user clicks on a remove button, the code sends a request to the server to remove that user from the DB first and then from the table list on the screen.

My problem is that a if else statment withint the ajax success does return what I want.

I can't add this code to fiddlers because it's linking to a database which is not accessible online.

$(nTd).children('a').click(function(e) {
    e.preventDefault();
    var that = $(this);
    if(that.hasClass('icon-i-remove')){
       $.ajax({
          url: $(this).attr('href'),
          type: $(this).attr('method'),
          dataType: 'json',
          success: function (data) {
            //PROBLEM HERE **********
            if(data.success == 'true' ){
               alert('true');
            } else {
               alert('false')
            };
       ;}
   });

So, 2 major problems. The if statement doesn't run correctly. even if it return true, the server jumps to the else without caring about it.

The second issue is this. When the user clicks on the remove button, the code should first check if the user has been deleted on the DB. If I have the ok from the DB, then I can remove the row from the table list without refreshing the page, otherwise I should display an error message (false return)

In my case, when I click remove, the code send a request to the server and doesn't care what the result is.

I need the code to wait before executing the client side remove. I need to know if that user has been removed from the db or not. I find it difficult to manage the if statement do. It seems it's not looking at the result back from the backend.

True or false it skips through either way. What's going wrong?

Upvotes: 0

Views: 44352

Answers (2)

Barry Chapman
Barry Chapman

Reputation: 6780

Is it actually returning 'true'? Or is it returning true? Two different things

UPDATE

$(nTd).children('a').click(function(e) {
    e.preventDefault();
    var that = $(this);
    if(that.hasClass('icon-i-remove')){
       $.ajax({
          url: $(this).attr('href'),
          type: $(this).attr('method'),
          dataType: 'json',
          success: function (data) {
            //PROBLEM HERE **********
            if(data.success == true ){   <-- Change to true (remove quotes)
               alert('true');
            } else {
               alert('false')
            };
       ;}
   });

Upvotes: 4

ilan berci
ilan berci

Reputation: 3881

If your ajax success callback is executing.. it means the server side operation was completed SUCCESFULLY.

If not, then the server should not send back a 200 that will get interpreted as a success

Upvotes: 0

Related Questions