2017-12-31 292 views
0

내가 바라 보았다 무엇 :response.chartAt (0)이 내 AJAX 호출에서 정의되지 않은 값을 반환하는 이유는 무엇입니까?

charAt coming back as undefined


질문 :

왜하면 String.charAt (0)이 경우에 정의되지 않은 반환하지?

내 응답의 첫 문자를 어떻게 반환합니까?


CODE :

서버

res.send("P This is a message"); //This is the response. 

클라이언트 가능성이 코드가 앱에 던져 오류를 억제가 존재한다

$.ajax({ 
     type: "POST", 
     url: "/1/2", 
     data: someData, 
    }).done(function(response) { 
     var message = String(response); 
     console.log("RESPONSE: "+message); 
     //Prints message without issue 
     console.log("RESPONSE FIRST LETTER :"+message.charAt[0]); 
     // 
     //Prints 'undefined' 
     // 
     if (message.charAt[0] == "P") { 
      localStorage.setItem("error_msg_local", message); 
     } else if (message.charAt[0] == "L") { 
      localStorage.setItem("success_msg_local", message); 
     } else { 
      localStorage.setItem("error_msg_local", "Internal error. Please try again."); 
     } 
    }); 

답변

0

. 이후,이 charAtString.prototype에 존재하는 방법 /함수이고, 그것은 괄호, 사각형이 브래킷을 사용하여 호출한다. 대신 방법의 배열 인덱스 접근로 chartAt 방법을 사용

// message.charAt[0] 
// should be 
message.charAt(0)