【问题标题】:JSON Unexpected token sJSON 意外令牌
【发布时间】:2016-10-09 06:38:12
【问题描述】:

JSON.parse() 似乎不适用于我的代码,我不确定是什么原因造成的。

JS代码:

var obj = JSON.parse(this.responseText);
console.log(obj.status + " " + obj.type);
if (obj.status == "success") {
   document.cookie = "accounttype=" + obj.type;
   document.cookie = "name=" + obj.name;
   var accounttype = getCookie(accounttype);
   if (accounttype == "Seller") {
      window.location.href = "../html/sell.html";
   }
}

PHP 代码:

$count = mysqli_num_rows($result);
    if($count == 1){
        echo '{"status": "success", "type":"$account", "name":"$name"}';
    }else{
        echo '{"status": "failed"}';
    }

希望大家能帮帮我,谢谢!

编辑更新的代码

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        var str = JSON.stringify(this.responseText);
        var obj = JSON.parse(str);
        console.log(obj.status + " " + obj.type);
        if (obj.status == "success") {
            document.cookie = "accounttype=" + obj.type;
            document.cookie = "name=" + obj.name;
            var accounttype = getCookie(accounttype);
            if (accounttype == "Seller") {
                window.location.href = "../html/sell.html";
            }
        } else {
            sweetAlert("Oops...", "Invalid username or password!", "error");
            document.getElementById("password").value = "";
        }
    }
}
xmlhttp.open("POST", "../php/login.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("username=" + username + "&" + "password=" + password);

这是更新后的 PHP 代码:

<?php
    include("checkdbconnection.php");

        session_start();


    $username = mysqli_escape_string($conn, $_POST['username']);
    $password = mysqli_escape_string($conn, $_POST['password']);
    $password = md5($password);

    $getLogin = "SELECT * FROM user WHERE username = '$username' and password = '$password'";
    $result = mysqli_query($conn, $getLogin);

    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $name = $row["firstname"];
    $account = $row["account_type"];

    if(!$result){
       echo "Query Error: ". mysqli_error($conn);
       die();
    }

    $jsonsuccess = ["status": "success", "type": $account, "name": $name];
    $jsonfail = ["status": "failed"];
    $jsonsuccstring = json_encode($jsonsuccess);
    $jsonfailstring = json_encode($jsonfail)
    $count = mysqli_num_rows($result);
    if($count == 1){
        echo $jsonsuccstring;
    }else{
        echo $jsonfailstring;
    }
?>

控制台为两个 JSON 返回值返回 undefined。

【问题讨论】:

    标签: php jquery mysql json visual-studio-cordova


    【解决方案1】:

    使用json_encode()

    您的服务器端响应应该是

     $json = ["status": "success", "type": $account, "name": $name];
    
     $jsonstring = json_encode($json);
     echo $jsonstring;
     die();
    

    你的 JS 代码应该是这样的

    $.ajax({  
        type: "GET",
        url: "URL.php",
        data: data,
        success: function(data){
            // Here is the tip
            var data = $.parseJSON(data);
            //Here is success 
            alert(data.status);
        }
    });
    

    编辑

    您应该验证您的 json 字符串。如果没问题,试试这个:

    var jsonStr="your json string";
    var json=JSON.stringify(jsonStr);
    json=JSON.parse(json)
    

    【讨论】:

    • 但是没有用,错误变为异常:意外的令牌
    • 你在console.log(this.responseText)上得到了什么?
    • 之前它是未定义的,但现在它甚至在打印之前就触发了错误。
    • 人的错误是什么?你能在你的问题中更新完整的代码jsfiddle吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2014-10-13
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多