U3F1ZWV6ZTc5OTgzNDMxMzE1MzJfRnJlZTUwNDYwNDY0NzA3Nzc=

How to correctly parse Json from php json_encode

How to correctly parse Json from php json_encode


Question :
I need to parse json information made by PHP json_encode in my front end application, I can't do this:
var data= '<?php echo $jsonarr; ?>';
<?php $arr_login= array("lname"=>"$rowlname","email"=>"$rowemail","fname"=>"$rowfname","mobile"=>"$rowmobile");
  echo json_encode($arr_login);
  ?>
My ajax code:
  $.ajax({
  type:"POST",
  url: url,
  data:data,
  success: function(data){
  var res = $.trim(data);
  if (res == "Password is inccorrect" || res== "Email is inccorrect") {
   $("#errmsg").html(data);
  }
    else{
    var response= JSON.parse(data);
    alert(response);
    //window.open("dashboard.html?sess=logined&","_self");

    }
}

    });

Presently in the event that I ready information it gives me the substantial json design as sent by PHP, however I have to change over it to javascript object so I can utilize it in DOM yet JSON.parse gives this mistake in comfort, if you don't mind help, grammar appears to be alright.

Solution :
The JSON.parse () method parses a JSON string and constructs the JavaScript value or the object described by this string. This function can optionally be used with a modification parameter allowing the object to be processed before it is returned.

The JSON.stringify () method converts a JavaScript value to a JSON string. Optionally, it can override values or specify the properties to include if an array of properties has been provided.

var response = JSON.parse(JSON.stringify(data));
if you want to improve the solution,
comment to help the community




Comments
No comments
Post a Comment

Post a Comment

NameEmailMessage