user1043568
user1043568

Reputation: 101

unable to test AJAX ResponseText

My PHP code responds to AJAX call with different strings, depending ... so I want to test the response, but I haven't been able to do it. I found that the responseText was of undefined type so tried type casting:

if (String(xmlhttp.responseText)=="OK")
{
    // do something
} else
{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}

Guess what gets displayed? OK. (Same without typecasting). Why?

Upvotes: 0

Views: 528

Answers (2)

user1400718
user1400718

Reputation:

Try the following:

var text = xmlhttp.responseText.trim();//use trim()

if (text=="OK"){
    // do something
}else{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}

Upvotes: 0

Peter
Peter

Reputation: 29837

If there really isn't more than we're seeing here, your string must either have whitespace or other non printable characters in it. Try trimming it before doing the comparison.

Edit Also try putting console logging in both your 'if' and 'else' blocks - maybe your method is being called multiple times unexpectedly and there is an odd race condition.

Upvotes: 1

Related Questions