SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post why i could not compare this string

    Hi, I am almost 4 hours debugging in this string,i could not compare this ..
    it always goes to my else statement but when i try to alert it is "success" but why it did not go to the if statement.please help me on this

    Thank you in advance.

    Code:
      $.ajax({
        type:'post',
        ....
        ...
        success:function(data){
          if( data == "success")
               alert('done');
          else
               alert(data + 'problem');
     }
    
    });

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    it always goes to my else statement but when i try to alert it is "success" but why it did not go to the if statement.please help me on this

    Code:
      $.ajax({
        type:'post',
        ....
        ...
        success:function(data){
          if( data == "success")
               alert('done');
          else
               alert(data + 'problem');
     }
    
    });
    What is the length of data? Is it more than 7 characters?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What is the length of data? Is it more than 7 characters?
    Yes, more than 7 ...i tried to alert(data.length)...why is it more than 7?it should be 7 characters.

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jemz View Post
    ...but when i try to alert it is "success"...
    Try console.log instead of alert. data might not be a string.

    EDIT: And continue to follow up with paul_wilkins's thought process. Seems there might be some whitespace characters in there. A newline maybe?
    "First make it work. Then make it better."

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already, tried console.log "success" but it always falls to else statement...I am confuse with this...

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there other way to check if this string contains whitespace or something...?

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Try console.log instead of alert. data might not be a string.

    EDIT: And continue to follow up with paul_wilkins's thought process. Seems there might be some whitespace characters in there. A newline maybe?

    I tried to use indexOf('\s')>=0 then it alerted..so it means has whitespace,... but i check this where it came from it has no whitespace.

    return "success" this is the string,i tried to retype.but it always fall to else statement....

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    I tried to use indexOf('\s')>=0 then it alerted..so it means has whitespace,... but i check this where it came from it has no whitespace.

    return "success" this is the string,i tried to retype.but it always fall to else statement....
    If it comes from a PHP file, does the PHP file end with ?> followed by a newline? Anything after the ?> will be output too.

    Code php:
    <?php
    echo "success";
    ?>
    Surprise!
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    If it comes from a PHP file, does the PHP file end with ?> followed by a newline? Anything after the ?> will be output too.

    Code php:
    <?php
    echo "success";
    ?>
    Surprise!


    Whoooo....thank you so much paul_wilkins...you solved my problem...now i know that if we put whitespace on this ?> end tag it will also output.

    many regards...


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
  •