1

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

1 Answer 1

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.