X9DESIGN
X9DESIGN

Reputation: 783

jquery ajax success action arguments not work correctly

Here is my ajax success function:

success: function (data) {
    if(data == 'logged') {
        window.location.href = window.location.href;
    } else {
        $("#result").html(data);
    }
},

In this action when my php script return 'logged' text redirect don't start but also script execute stuff from else argument and insert to my #result div returned 'logged' text.

This also happen when my php script don't return 'logged' text but return other stuff.. returned data is inserted to #result div ... seems this function completly skips the if/else arguments.. why it happen?

Upvotes: 2

Views: 197

Answers (1)

Shyju
Shyju

Reputation: 218722

The only reason i can think is you might be getting a space with the word logged. Ideally you should fix that in your server side to send the trimmed/correct response. But if you want to do that in the client side for any reason, you may use the trim().

The trim() method removes whitespace characters like space, tab and no-break space etc from both ends of a string. method. Also consider using === (Identity operator)for the comparison when you know both operands are of same type( In our case both are string type).

success: function (data) {
    if(data.trim() === 'logged') {
        window.location.href = window.location.href;
    } else {
        $("#result").html(data);
    }
},

Upvotes: 4

Related Questions