使用Newtonsoft.Json发送的jQuery处理json数据。

时间:2023-02-10 23:47:20

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

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.

console.log(list.Person.length);返回未定义的,因此不进入for循环。所以,为了解决这个问题。为什么需要指定contentType而数据类型已经存在。我也可以用$。getJSON代替美元. ajax。

2 个解决方案

#1


3  

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

您应该将您的代码更改为var list ={“Person”:data.d};以反映你的警觉。

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");
                }
            });
        }  

#2


0  

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

这也应该是GET请求而不是post,这样你就可以使用$. getjson。

#1


3  

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

您应该将您的代码更改为var list ={“Person”:data.d};以反映你的警觉。

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");
                }
            });
        }  

#2


0  

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

这也应该是GET请求而不是post,这样你就可以使用$. getjson。