Tuesday 15 March 2011

value undefined while decoding json with jquery -


I know there are already many questions related to this subject, but I have tried many of them but I am still Also unable to get the value

I am using Reebok and I am getting the right response: -

  [{"id": 4, "comments": 1, "likes" : 5, "books": 3, "name": "steve"}]   

but I'm not able to display them on the page. When I try to alert (data. Id) then I get undefined in the warning box. Here's my code: -

  $ Ajax ({type: "GET", url: "getdata.php", data: datestring, datatype: "Jason", success: function (data) {warning (data.id);}});   

I tried

  $ Each (data, function (key, element) {warning (key); alert (element);});   

This key shows as 0, but in the warning box as the object [object object]

That's because your feedback is an array.

Either

  success: function (data) {warning (data [0] .id); }   

or only one object return

  {"id": 4, "comments": 1, "likes": 5, "books": 3, "name": "steve"}   

or if you want to use an each array like

  $ Each (data, function (key, element) {warning (key); alert (element.id); alerts (element.comments); alerts (element.likes); ...});    

No comments:

Post a Comment