Q:

Cant get value from json in javascript

I have a function in 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;
}

And the server side returns the json data like this:
$return=array("status"=>"true","user"=>$user,"fname"=>$fname,"middle_name"=>$middle_name,"lname"=>$lname);
$return=json_encode($return);
echo $return;

Now the alert should give the alert true but it doesn't work, I hope I am able to make you clear

A:

Use JSON.parse(text) before your alert(text.status) for encode string in JSON object
http.onreadystatechange=function(){
    if(http.status==200 && http.readyState==4) {
        text=JSON.parse(http.responseText);
        alert(text.status);
    }
}

