0

Hello guys i'm working with user login and when an user submits the login credentials an API is being called using ajax and if the login credentails are correct/ wrong i'm getting the response and after stringifying the data is displayed in the following format in console.

If true {"$P_SUCCESS":true}

If false {"$P_SUCCESS":false}

Now i want to read the value true from the above string and want to redirect an user to a new page based on the condition.

I tried different options but all in vain.

Thanks for your help in advance.

2
  • 1
    show the code you tried. Commented Apr 8, 2015 at 6:05
  • var redirect = JSON.parse('{"$P_SUCCESS":true}'); Commented Apr 8, 2015 at 6:05

4 Answers 4

2

You can access the property of the object by specifing the name between [] (ex: object['name']).

var redirect = JSON.parse('{"$P_SUCCESS":true}')
var success = redirect['$P_SUCCESS']; // get value of $P_SUCCESS
alert(success);

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

Comments

1

Try this

var data = {"$P_SUCCESS":true};
var result = data['$P_SUCCESS'];
if(result)
{
    //redirect
}

Comments

0
var r = eval(...the response...); // or whatever json decoding function
if(r.$P_SUCCESS) document.location = 'http://...';
else document.location = 'http://...';

4 Comments

eval? so it would parse any JavaScript as long as it comes as a result? a little more security please
No i would use the function provided by the framework I use... and since i dont know what he is using... (see the comments)
Only if vanilla JS had something to do that... :P
vanilla JS is not the same from IE6 to Chrome19... That's why we use js libraries... no?
0

JSON.parse() is the thing you are looking for, it will parse you the data in the JSON object from where you can read the data easily and redirect the user accordingly.

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.