StudioTime
StudioTime

Reputation: 23999

JQuery ajax success not reading text correctly

I have a simple ajax call to a PHP script, when the PHP script is finished correctly it simply echos "ok".

If it is "ok" I want something to happen, or else something else to happen...

Problem is that the === is not matching the text and I get error alert each time although firebug shows me "ok" is being returned and in fact the PHP script is working perfectly.

Here's the JQuery

$.ajax({
    url: '/ajax/actions/imageUpload.php?act_id=<?=$act_id?>',
    type: 'POST',
    success: function (data) {
        if(data==='ok'){
            alert('all is good');
        } else {
            alert('error');
        }
    }
});

Why is if(data==='ok') failing?

Screenshot below, I changed code to:

success: function (data) {
        if(data==='ok'){
            alert('all is good');
        } else {
            alert(data);
        }
    }

As you can see it alerts data and not "all is good", yet the data is "ok"

enter image description here

Upvotes: 0

Views: 385

Answers (1)

Laxmikant Ratnaparkhi
Laxmikant Ratnaparkhi

Reputation: 5023

Try this:

$.ajax({
    url: '/ajax/actions/imageUpload.php?act_id=<?=$act_id?>',
    type: 'POST',
    success: function (response) {
        if(typeof response == typeof 'string'){
            if( response == 'ok')
                 alert('all is good');
             else
               alert('error:');
        } else {
            alert('error: Type Mismatch');
        }
    }
});

You can remove the line typeof response == typeof 'string' once you test it.

Upvotes: 1

Related Questions