Json.parse () "Parse a JSON object from a string"
Json.stringify () "Parse out a string from an object"
To do the project landing function, the use of the H5 Sessionstorage method, will be logged back after the information to save to the browser. But the return information is an object, so you need to use
The Json.stringify method converts an object into a string and then stores it. as the following code
//Click to loginfunction Fun_login (_type) {//Check Entry items: varErr =Checklogin (); if(ERR) {alert (ERR); return; } varFormData = { "Logintype": _type,"LoginName": $(". Login-right input[name= ' loginname ']"). Val (),"Password": $(". Login-right input[name= ' password ']"). Val ()}; $.ajax ({type:'Post', URL:".. /spirit/login", Data:formdata, DataType:'JSON', Success:function (data) {Console.log (data); if(Data.returncode = ="xx") {IsLogin="1"; varMemberobjstr =json.stringify (Data.memberinfo); Sessionstorage.setitem ("MemberInfo", MEMBEROBJSTR); Sessionstorage.setitem ("pwd", $(". Login-right input[name= ' password ']"). Val ()); $("#signBox"). HTML (""); $("#signModal"). Modal ("Hide"); Fillmembermes (MEMBEROBJSTR); } Else{alert ("Login failed:"+data.messageinfo); }}, Error:function (XMLHttpRequest, Textstatus, Errorthrown) {//Failure callback function varm ="system error: \nstatu="+ Xmlhttprequest.status +"\nreadystate="+ Xmlhttprequest.readystate +"\ntext="+ Textstatus +"\nerrthrown="+Errorthrown; Alert (m); } });
When the login after the refresh, the browser to determine the information stored in the absence, if any, then put personal information on the top of the site, if not, then remind login. At this time to fill the information with the object to fill, so the Json.parse method to convert the string to an object and then operate;
// Loading Information $ (document). Ready (function (e) { var memberobjstr = Sessionstorage.getitem (" MemberInfo"); if (MEMBEROBJSTR) fillmembermes (MEMBEROBJSTR); });
Fill header information after landing
function Fillmembermes (data) {
var data = json.parse (data);
Console.log (data);
if (data) {
if (Data.memberclass = = ' 1 ') Data.membertype = "Individual Member";
else {
if (Data.membertype = = ' 1 ') Data.membertype = "Supplier Member";
Else
if (Data.membertype = = ' 2 ') Data.membertype = "Purchasing Unit Member";
Else
Data.membertype = "Unknown type member";
}
var str = ' <span class= ' headimg "> ' + data.username + ' </span> ' +
' <span> ' + ' (' + Data.membertype + ') ' + ' </span> ' +
' <a onclick= ' toadmin () "style=" color: #fc771f;p adding-left:15px;cursor:pointer; " > Log in to the management side </a> ';
$ ("#signBox"). html (str);
};
}
Json.parse () and json.stringify () usage