0

次のコードを使用して Web メソッドへの AJAX 呼び出しを正常に行うことができ、Web メソッドは以下に貼り付けられた JSON を返します。

マイ Web メソッド

[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

データを使用: "d"スキーマセクションの下。それはうまくいくはずです。

于 2013-08-02T09:59:19.287 に答える