Results 1 to 6 of 6

Thread: Doing a string comparison on reponseText :(

  1. #1
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Doing a string comparison on reponseText :(

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Oct 2005
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    alert('SUCCESS!');

    maybe?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yep, that's valid.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •