c.k c.k - 5 months ago 13
PHP Question

Parse JSON using javascript and get specific value in array

In my console log the array look like this:

{"userid":"5502","fullname":"My fullname","email":"sample@yahoo.com","user_access":"real"}


Now on my ajax, I have a handle code for the data array that the server sends to the app:

function handleData(responseData) {
var access = responseData;

console.log(access);
if (access == '"real"') {
alert("Welcome");
location.href = "home.html";
} else {
alert("Your username and password didn\'t match.");
}
}


How I can get the specific value of this
"user_access":"real"
in array and use it in condition.

like this:

if (access == '"real"') { // What should be the format of access variable?
alert("Welcome");
location.href = "home.html";
}

Answer
function handleData(responseData) {
                var response = JSON.parse(responseData);//assuming you are getting the response as a string

                var access = response.user_access;    
                console.log(access);

                if (access == "real") {
                    alert("Welcome");
                    location.href = "home.html";    
                } else {
                    alert("Your username and password didn\'t match.");
                }    
            }//handleData()