2017-12-31 148 views
-6

QUESTION:いくつかの奇妙な理由でJS!レスポンスでP!= "P"が表示されるのはなぜですか?

は、私のJSONから "P" がtrueを返す "P" が、 "P" == "P" に等しいではないようです。これは意味がありません。

どこかのデータ型の問題があるはずです。

どうすればresponse.charAt(0) == "P"が返されるのを確認できますか?true


CODE:

$.ajax({ 
     type: "POST", 
     url: "/1/1", 
     data: someData, 
    }).done(function(response) { 
     console.log("p" == "P"); //prints false 
     console.log("P" == "P"); //prints true 
     console.log("RESPONSE: "+response); //prints "Primary" 
     console.log("RESPONSE FIRST LETTER: "+response.charAt(0)); //prints P 
     console.log("RESPONSE BOOL P :"+response.charAt(0)=="P"); 
     // 
     //false for some reason, should be true 
     // 
     if (response.charAt[0] == "P") { 
      console.log("1"); 
      localStorage.setItem("error_msg_local", message); 
     } else if (response.charAt[0] == "L") { 
      localStorage.setItem("success_msg_local", message); 
      console.log("2"); 
     } else { 
      localStorage.setItem("error_msg_local", "Internal error. Please try again."); 
      console.log("3"); 
      // 3 gets logged when it should have been 1 
     } 
    }); 
+0

質問は何ですか – Sajeetharan

+0

なぜdownvoteですか? –

+0

@Sajeetharan "どうすればresponse.charAt(0)==" P "がtrueを返すのですか? –

答えて

4
response.charAt[0] 

配列としてcharAtことを扱います。 ()[]の差異に注意してください。

charAtを使用するには括弧を使用する必要があります。機能

response.charAt(0) 
+0

どうも。 –

1

あなたはコメントで述べたように、それは.charAt

あるべき変更

if (response.charAt[0] === "P") 

から

if (response.charAt(0) === "P") 
関連する問題