M.Nabeel
M.Nabeel

Reputation: 1066

Undefined variable $.parseJSON

My authenticate.php file is returning an object

Json: [{"u_id":"1","u_name":"Nabeel" }]

Here is my js:

success: function(result) {
  var obj=$.parseJSON(result);
  alert(result);      //[{"u_id":"1","u_name":"Nabeel" }]
  alert(obj.u_id);    //undefined
  window.location="#timeline";
}

I want to save u_id and name in a separate variables but I keep on getting undefined as a result.

I tried using the jQuery.parseJSON function as well but I was getting "undefined variable Jquery"

Upvotes: 2

Views: 130

Answers (1)

jeffjenx
jeffjenx

Reputation: 17457

Your PHP script is returning an array so you need to provide the index or fix your PHP output.

console.log(obj[0].u_id);

Upvotes: 6

Related Questions