2012-05-02 28 views
0

jquery ajax関数では、サーバーの "true"または "false"文字列を受け取ります。私は結果を知っているだけですが、条件が満たされていれば偽になります。JavaScriptを比較文字列

$.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'); 
      } 
     } 
    }) 
... 

...

誰かが私を助けることができますか?

+1

もしあなたのインタプリタ'result ==" true "'が 'false'ならば' result'は '' true ''ではないと言っています。結果は、本当に保持されています。 'console.log(result)'を試してください。 – Yoshi

+0

本当に結果に「真」が含まれていますか? –

+0

あなたはjQueryを使用していますが、jQueryは結果の型を特定するために「スマートな推測」を使用することがあります。 'result === true'を試してください。ブール値に変わったかもしれません。 – Joseph

答えて

5

改行やで余分なスペースがあるかもしれません」結果"。

+0

あなたのための賞品!私はJSで問題を探しているだけですが、print()関数をprint()関数を使ってjavaサーブレットの応答に使っていました。 – Dani

0

うーん、

空の文字列は、null、0および未定義の

result2==="true"すべて偽であるが、より良いテスト

DEMOある

0

使用トリム()関数...

例:

$アヤックス({

...

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'); 
     } 
    } 
}) 

...