2

I have tried searching all possible matches of my problem and also have tried a couple of solutions but unfortunately none worked
My backend code:

Person p;
        foreach(DataRow dr in dt.Rows)
        {
            p = new Person();
            p.id = Convert.ToInt16(dr["Id"]);
            p.name = dr["Name"].ToString();
            p.phone = Convert.ToInt64(dr["Phone"]);

            pList.Add(p);
        }
        string ans = JsonConvert.SerializeObject(pList, Formatting.Indented);  

jQuery.ajax

function ShowData() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/Data",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    alert(data.d);
                    var list = { "Person": +data };
                    for (i = 0; i < list.Person.length; i++) {
                        alert('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone);
                        console.log('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone);
                    }
                    console.log(list.Person.length);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }  

Alert output

[

  {

    "id": 1,

    "name": "Bhavik",

    "phone": 9601109585

  },

  {

    "id": 2,

    "name": "Xyz",

    "phone": 1234567890

  },

  {

    "id": 3,

    "name": "Abc",

    "phone": 9876543210

  }

]  

console.log(list.Person.length); returns undefined and hence does not enters the for loop.. So to work out with it.. and why is it necessary to specify contentType while dataType already exist.. Also can I use $.getJSON instead of $.ajax.

Rooster
  • 9,954
  • 8
  • 44
  • 71
Bhavik
  • 4,836
  • 3
  • 30
  • 45
  • 1
    From your `alert`, it looks like `{"Person": +data}` should be `{"Person": data.d}`, shouldn't it? – Kyle Jul 19 '13 at 19:58

2 Answers2

3

You should change your code to be var list = {"Person": data.d}; to reflect what you're alerting.

function ShowData() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/Data",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    alert(data.d);
                    var list = { "Person": +data.d };
                    for (i = 0; i < list.Person.length; i++) {
                        alert('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone);
                        console.log('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone);
                    }
                    console.log(list.Person.length);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }  
Rooster
  • 9,954
  • 8
  • 44
  • 71
  • Just to add a little to your answer, placing the `+` in front of a variable will retrieve the integer value of that variable: (`+'123'` becomes `123`). If the variable is not a valid integer (`abc123`), you will get back `NaN`. What the OP has created in `list` will look like `{"Person":NaN}`. – Kyle Jul 19 '13 at 20:05
  • @Rooster shit man what a mistake.. I thought like code in C# and added `+` to concatenate.. Done man.. Thanks.. But than `list.Person.length` will count the char and I need only to count the set of answer that must be 3 here.. Any ideas.. – Bhavik Jul 19 '13 at 20:11
  • @Bhavik remove the + and set the contents of list using an if to check to make sure data.d is what you want. like if(typeof data.d === 'object'){var list = { "Person": data.d };}else{var list = {};} or something like that?? – Rooster Jul 19 '13 at 20:16
0

Also this should be a GET request not a post, then you would be able to use $.getJSON.

Justen Martin
  • 626
  • 1
  • 6
  • 13
  • I tried using `$.parseJSON` but it didn't worked.. And do you mean that $.getJSON works with GET only.. Thanks – Bhavik Jul 19 '13 at 20:04
  • actually when you declare a dataType = json it expects a json in return and attempts to parse it. – Rooster Jul 19 '13 at 20:12
  • @Rooster you said `it attempts to parse it` but is it necessary that it does it all times.. Because my experience says something else [Check my question](http://stackoverflow.com/questions/17755523/looping-within-list-using-jquery/).. Hope I am not wrong.. – Bhavik Jul 19 '13 at 22:16