为什么P!=" P"在我的JSON响应中?

时间:2017-12-31 16:38:32

标签: javascript jquery json ajax string

问题:

出于某种奇怪的原因,似乎" P"来自我的JSON不等于" P"但是" P" ==" P"返回true。这毫无意义。

某处必须存在某些数据类型问题。

如何确保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
        }
    });

2 个答案:

答案 0 :(得分:4)

response.charAt[0]

charAt视为数组。请注意()[]之间的差异。

您应该使用括号来使用charAt。功能

response.charAt(0)

答案 1 :(得分:1)

正如您在评论中提到的,它应该是 .charAt

更改

发件人

case "META-INF/services/org.apache.spark.sql.sources.DataSourceRegister" => MergeStrategy.concat

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