0

我可以使用以下代码成功地对我的 Web 方法进行 AJAX 调用,并且 Web 方法返回粘贴在下面的 JSON:

我的网络方法

[WebMethod]
public static string GetJson()
{
     string query = "SELECT top 10 cast(ClientUID as varchar) ClientUID FROM <tablename>";
     SqlCommand cmd = new SqlCommand(query);
     // Populate the DataSet.
     DataSet data = GetData(cmd);
     // return the Customers table as JSON.
     DataTable dt = data.Tables[0];
     var returnJSON = (from p in dt.AsEnumerable()
          select new
          {
               ClientUID = p.Field<string>("ClientUID")
           }).ToList();
      var serializer = new JavaScriptSerializer();
      string json = serializer.Serialize(returnJSON);
      return json; 
}

Web 方法返回的 JSON:

[{"ClientUID":"1"},{"ClientUID":"2"},{"ClientUID":"3"},{"ClientUID":"4"},{"ClientUID":"5"} ,{"ClientUID":"6"},{"ClientUID":"7"},{"ClientUID":"8"},{"ClientUID":"9"},{"ClientUID":"10"} ]

使用 AJAX 调用 Web 方法

<script type="text/javascript">
        $(document).ready(function() {
            $.ajax(
                    {
                        type: "POST",
                        url: "ServeClientCalls.aspx/GetJson",
                        data: {},
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function(msg) {
                        //checking the content return from the above web method
                        $("#msg").html(msg.d); 
                            //Binding to kendo Grid
                            $("#grid").kendoGrid({
                                dataSource: {
                                    data: msg.d,
                                    schema: {
                                        model: {
                                            fields: {
                                                ClientUID: { type: "string" }
                                            }
                                        }
                                    },
                                    pageSize: 20
                                },

                                height: 430,
                                filterable: true,
                                sortable: true,
                                pageable: true,
                                columns: [
                                        { field: "ClientUID" }
                                    ]
                            });
                        },
                        error: function(x, e) {
                            $("#msg").html(x.responseText);
                        }
                    });
        });
    </script>

问题:我的网格没有绑定,只显示标题,而当我以下面提到的这种方式使用代码时,它正在工作

<script type="text/javascript">
        $(document).ready(function() {
            $.ajax(
                    {
                        type: "POST",
                        url: "ServeClientCalls.aspx/GetJson",
                        data: {},
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function(msg) {
                        //checking the content return from the above web method
                        $("#msg").html(msg.d);
                        **var p = [{ ClientUID: 1 }, { ClientUID: 2 }, { ClientUID: 3 }, { ClientUID: 4 }, { ClientUID: 5 }, { ClientUID: 6 }
                            , { ClientUID: 7 }, { ClientUID: 8 }
                            , { ClientUID: 9 }, { ClientUID: 10}];**
                            //Binding to kendo Grid
                            $("#grid").kendoGrid({
                                dataSource: {
                                    **data: p,**
                                    schema: {
                                        model: {
                                            fields: {
                                                ClientUID: { type: "string" }
                                            }
                                        }
                                    },
                                    pageSize: 20
                                },

                                height: 430,
                                filterable: true,
                                sortable: true,
                                pageable: true,
                                columns: [
                                        { field: "ClientUID" }
                                    ]
                            });
                        },
                        error: function(x, e) {
                            $("#msg").html(x.responseText);
                        }
                    });
        });
    </script>
4

1 回答 1

0

使用data: "d",在模式部分下。那应该行得通。

于 2013-08-02T09:59:19.287 回答