无法从json中获取json的价值

时间:2012-12-12 12:41:21

标签: javascript json

我在JavaScript中有一个函数:

function login() {
    user=document.getElementById("user_id").value;
    pass=document.getElementById("password").value;
    params="user="+user+"&pass="+pass;
    url="check_login.php";

    if(window.XMLHttpRequest) {
        var http=new XMLHttpRequest();
    } else {
        var http=new ActiveXObject("Microsoft.XMLHTTP");
    }
    var http=new XMLHttpRequest();

    http.onreadystatechange=function(){
        if(http.status==200 && http.readyState==4) {
            text=http.responseText;
            alert(text.status);
        }
    }
    http.open("POST",url,true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(params);
    return false;
}

服务器端返回json数据,如下所示:

$return=array("status"=>"true","user"=>$user,"fname"=>$fname,"middle_name"=>$middle_name,"lname"=>$lname);
$return=json_encode($return);
echo $return;

现在警报应该提醒警报但它不起作用,我希望我能让你清楚

1 个答案:

答案 0 :(得分:2)

JSON.parse(text)之前使用alert(text.status)获取JSON对象中的编码字符串

http.onreadystatechange=function(){
    if(http.status==200 && http.readyState==4) {
        text=JSON.parse(http.responseText);
        alert(text.status);
    }
}