2

In my console log the array look like this:

{"userid":"5502","fullname":"My fullname","email":"[email protected]","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";
}
11
  • 1
    if (access.access == "real") .... Commented Jun 6, 2016 at 8:15
  • 2
    responseData.access === 'real' Commented Jun 6, 2016 at 8:15
  • 4
    That is not an array but object..Use Property accessors to access the property of object Commented Jun 6, 2016 at 8:15
  • 1
    Your question (the example object) is inaccurate. You've asked how to check the access property, when you actually wanted the user_access property. Commented Jun 6, 2016 at 9:55
  • 1
    @evolution, yes my wrong inputting the real object I used on my code. Thanks anyway. Commented Jun 7, 2016 at 0:25

2 Answers 2

4
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()

Normally, we want our response to be in json ( or we can say 'object' ) form, so that we can easily access its inner properties. So, if it's already an object, you do not need to use JSON.parse. You can directly access any property like this - responseData.user_access . But if it's in string form, then you have to use JSON.parse() first to parse the string into JSON ( or object ) format.

Sign up to request clarification or add additional context in comments.

8 Comments

console.log(access); it is undefined.
can you please paste the output of these two lines: console.log( responseData ) ; console.log( typeof responseData );
console.log( responseData ) ; --> {"user_id":"5502","user_fullname":"My fullname","user_email":"[email protected]","user_access":"real"} console.log( typeof responseData ); --> string
ok, so it does not have access property, but user_access. I have updated my answer accordingly. Please try once again.
Hi @Mike,sorry just got back to work. It is working now thank you for helping me out. My mistake I write wrong object on my example. It should be user_access not access.
|
1

If it is not surrounded by "" around the {} brackets then just do

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

1 Comment

It is undefined when I try to display (access) it in console log.

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.