简体   繁体   中英

JSON data retrieval using javascript

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.

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); 

Try this

var data = {"$P_SUCCESS":true};
var result = data['$P_SUCCESS'];
if(result)
{
    //redirect
}
var r = eval(...the response...); // or whatever json decoding function
if(r.$P_SUCCESS) document.location = 'http://...';
else document.location = 'http://...';

JSON.parse()是您要寻找的东西,它将解析JSON对象中的数据,从中可以轻松读取数据并相应地重定向用户。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM