My JavaScript code is not able to read the JSON generated by my PHP code. What is wrong? -
i'm using ajax call , i'm generating json object sent javascript. when receive in javascript i'm not able values it.
php:
echo json_encode(array("results" => array(array("user" => $member['user']),array("company" => $member['company']))));
js:
success: function(response) { for(var = 0;i < response.results.length; i++) { sessionstorage.setitem('user',response.results[i].user); sessionstorage.setitem('company',response.results[i].company); } }
i'm not able read values response receive.
response in format:
{"results":[{"user":"david"},{"company":"something"}]}
what proper way of reading json object?
since trying access results[0].company , results[1].user undefined, , since results[1].company , results[0].user defined in object, if change php follows javascript should work unless response string:
echo json_encode( array( "results" => array( array( "user" => $member['user'], "company" => $member['company'] ) ) ));
if response string change javascript code follows:
success: function(response) { response = json.parse(response);
or
datatype: "json", success: function(response) {
Comments
Post a Comment