Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么response.chartAt(0)在我的AJAX调用中返回undefined?_Javascript_Jquery_Json_Ajax_String - Fatal编程技术网

Javascript 为什么response.chartAt(0)在我的AJAX调用中返回undefined?

Javascript 为什么response.chartAt(0)在我的AJAX调用中返回undefined?,javascript,jquery,json,ajax,string,Javascript,Jquery,Json,Ajax,String,我所看到的: 问题: 在这种情况下,为什么String.charAt(0)返回undefined 我如何让它返回我回复的第一个字母 代码: 服务器 res.send("P This is a message"); //This is the response. 客户 $.ajax({ type: "POST", url: "/1/2", data: someData, }).done(function(response) {

我所看到的:


问题:

在这种情况下,为什么String.charAt(0)返回undefined

我如何让它返回我回复的第一个字母


代码:

服务器

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

可能存在抑制应用程序上抛出的任何错误的代码。由于,
字符串上存在方法/函数。原型
,因此应使用括号调用它,而不是方括号

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

如果您使用chartAt方法作为数组索引访问器而不是方法,正确的用法应该是message.charAt(0)


哈哈哈。真不敢相信我漏掉了那个打字错误。谢谢!