2017-12-31 74 views
0

私が見WHAT:なぜAJAX呼び出しでresponse.chartAt(0)が未定義で返されますか?

charAt coming back as undefined


QUESTION:

なぜString.charAtは(0)この場合はundefinedを返すのですか?

返信の最初の文字を返すにはどうすればよいですか?


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

charAtはがString.prototypeに存在方法/関数であるので、括弧なく、角括弧を使用することによって呼び出されるべきです。かわりの方法の配列インデックスアクセッサとしてchartAt法を使用している

// message.charAt[0] 
// should be 
message.charAt(0) 
関連する問題