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.

1
  • 1
    From your alert, it looks like {"Person": +data} should be {"Person": data.d}, shouldn't it? Commented Jul 19, 2013 at 19:58

2 Answers 2

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");
                }
            });
        }  
Sign up to request clarification or add additional context in comments.

3 Comments

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}.
@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 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??
0

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

3 Comments

I tried using $.parseJSON but it didn't worked.. And do you mean that $.getJSON works with GET only.. Thanks
actually when you declare a dataType = json it expects a json in return and attempts to parse it.
@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.. Hope I am not wrong..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.