2017-12-31 59 views
-6

질문 : 어떤 이상한 이유로왜 P! = "P"가 내 JSON 응답에 있습니까?

는, 내 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") 
관련 문제