0

In jquery ajax function I recive to the server "true" or "false" strings. Then I only wnat know result but only get false in if condition.

 $.ajax({
    ...
        success: function(result) {
              if(result == "true"){   // false???????
                    alert('true');
                } else {
                    alert('false');
                }
             }
        })
    ...

$.ajax({
...
    success: function(result) {
          var result2 = String(result); 
          alert(result2);          // true
          alert(typeof result2);   // String
          alert(typeof "true");    // String
          if(result2 == "true"){   // false???????
                alert('true');
            } else {
                alert('false');
            }
         }
    })
...

...

Somebody can help me?

3
  • 1
    If your interpreter says that result == "true" is false then result is not "true". Simple as that. You might want to check if your assumption of what result is, are really holding. Try console.log(result). Commented May 2, 2012 at 9:38
  • Are you sure result contains "true" ? Commented May 2, 2012 at 9:38
  • you're using jQuery, and jQuery uses "smart guesses" sometimes to figure the result type. try result === true. It might have turned into boolean. Commented May 2, 2012 at 9:40

3 Answers 3

5

There could be line breaks or extra spaces in "result".

Sign up to request clarification or add additional context in comments.

1 Comment

Prize for you! I only looking for problem in JS, but I was using println() function instead print() to response in java servlet.
0

Hmm,

An empty string, null, 0 and undefined are all false

result2==="true" is a better test

DEMO

Comments

0

use trim() function...

e.g:

$.ajax({

...

success: function(result) {
      var result2 = String(result.trim()); 
      alert(result2);          // true
      alert(typeof result2);   // String
      alert(typeof "true");    // String
      if(result2 == "true"){   // true
            alert('true');
        } else {
            alert('false');
        }
     }
})

...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.