ZedBee
ZedBee

Reputation: 2378

$.ajax() - working with returned json data

I am unable to extract data form the returned json data from ASP.NET webmethod. Here is my code:

$.ajax({
    type: "POST",
    url: "myurl",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (appdata) {
        console.log(appdata.d);
        var testdata = appdata.d;
        //$.each(testdata, function (index, appt) {
        //    console.log(appt);
        //});
    }
});

console.log(appdata.d) returned data in following format

[
 {
  'PRODUCTID' : '51',
  'ENTRYDATE' : '2/13/2013 12:00:00 AM',
  'CATEGORYID' : ''
 },

 {
  'PRODUCTID' : '52',
  'ENTRYDATE' : '2/13/2013 12:00:00 AM',
  'CATEGORYID' : ''
 }
]

code

//$.each(testdata, function (index, appt) {
//    console.log(appt);
//});

resulted in following error:

Uncaught TypeError: Cannot use 'in' operator to search for...

How can I extract this data??

Edit console.log(appdata) gives the following

Object {d: "[{'PRODUCTID' : '51','ENTRYDATE' : '2/13…ENDTIME' : '2/13/2013 3:45:00 AM','CATEGORYID' : ''}]"}

Upvotes: 0

Views: 746

Answers (1)

JJJ
JJJ

Reputation: 33153

I suspect that if you console.log( appdata ) you get something like this:

{
    d: "[
    {
      'PRODUCTID' : '51',
      'ENTRYDATE' : '2/13/2013 12:00:00 AM',
      'CATEGORYID' : ''
    },
    ..."
}

That is, the server is wrapping the array contained in d in quotes. The response is valid JSON so jQuery doesn't complain about it, but the value of d is not an object but another JSON string.

Either fix the server script so that it doesn't do that, or if that's not possible, parse the string as a separate JSON entity.

Upvotes: 2

Related Questions