PDA

View Full Version : Doing a string comparison on reponseText :(



bughead
10-01-2006, 11:47 AM
Hi Everyone,

I'm fairly new to javascript but I think what I'm trying to do should be quite simple. I've been banging my head on this for the couple of hours though I'm really not sure why its not working.

I'm trying to do a simple string comparison on an ajax plain text response but for some reason I cannot get the string comparison to be true.

I have a XMLHttpRequest object that retrieves a plain text file. In this case the file contains 1 line with the number 100 in it. There are no extra characters in it.

When I retrieve the responseText and display it in an alert box it indeed says its 100.

But for some reason I cannot get the following code to work.

if (xhrRec.responseText == "100") {
alert(SUCCESS!);
}

Can anyone please suggest why this doesn't evaluate to be true when xhrRec.responseText appears to be 100. Any help is greatly appreciated.
Thanks.
Bug.

Twey
10-01-2006, 01:50 PM
alert(SUCCESS!) is not valid syntax.

Other than that, we'd have to have a link to the text file in question to know for sure.

Eclyps19
10-01-2006, 02:05 PM
alert('SUCCESS!');

maybe?

Twey
10-01-2006, 02:19 PM
Yep, that's valid.

bughead
10-01-2006, 02:54 PM
Sorry guys,

That was a bad example. What's actually executed isn't important. The problem is that it evalutes as false rather than true. If I put an else statement in it will be executed.

The file itself only has 3 characters in it (100). So what I'm wondering is there any possibilities that when the XMLHttpRequest object (xhrRec) is being returned is it possible that it's being returned with hidden characters or tags in it that I cannot see that may be responsible for it coming back as false.

How could I go about debugging this to see what xhrRec.responseText is "exactly"?

If I do alert(xhrRec.responseText); it pops up an alert that says 100. Nothing else.

Any ideas?

Cheers,
Bug

Twey
10-01-2006, 03:21 PM
There could well be other characters in the file (line breaks on the end, for example). Try alerting xhrRec.responseText.length and see if it says 3.

As I said, to make an accurate judgement would require seeing the text file.