Json C# WebMethod 的输出不影响 JQuery 的数据表

Json Output of a C# WebMethod Does Not Affect a JQuery's DataTable

我已经被这个问题困扰了一段时间,每次都会遇到不同的错误或某种无法帮助我实现目标的问题。

我正在为 jQuery 使用 table plug-in,但似乎无法从我 ASP.NET WebMethod 那里得到正确的 Json 响应适合 table 的数据。我的 table 的状态停留在 "Loading.." 并且似乎什么也没有发生,即使我确实从我的 WebMethod 收到了正确的 Json 响应。它似乎并没有影响我的数据表,或者只是由于某些问题在初始化期间冻结了它。

我的代码:

Client-Side:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="DataTables-1.10.4/media/css/jquery.dataTables.css" />
    <link rel="stylesheet" href="DataTables-1.10.4/extensions/Scroller/css/dataTables.scroller.css" />
</head>
<body>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>first name</th>
                <th>last name</th>
                <th>position</th>
                <th>office</th>
                <th>start_date</th>
                <th>salary</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th>first name</th>
                <th>last name</th>
                <th>position</th>
                <th>office</th>
                <th>start_date</th>
                <th>salary</th>
            </tr>
        </tfoot>
    </table>
    <script src="jquery-1.11.1.min.js"></script>
    <script src="DataTables-1.10.4/media/js/jquery.dataTables.min.js"></script>
    <script src="DataTables-1.10.4\extensions\Scroller\js\dataTables.scroller.js"></script>
    <script>
        $('#example').dataTable({
            //"ajax": "ajaxtest.txt",
            "ajax": {
                "url": "WebService.asmx/GetTable",
                "type": "POST"
            },
            "deferRender": true
        });
    </script>
</body>
</html>

我的WebService.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebService : System.Web.Services.WebService {

    public WebService () {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetTable() {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string response = js.Serialize(Person.GetPersons());

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-length", response.Length.ToString());
        Context.Response.Flush();

        Context.Response.Write(response);
    }
}

我的Json回复(有效):

[{"first_name":"Thomas","last_name":"Vincunas","position":"Manager","office":"066843-4120","start_date":"12/12/1995","salary":"4,081"},{"first_name":"David","last_name":"Naidich","position":"Worker","office":"052-343-4120","start_date":"12/11/1990","salary":"4,081"},{"first_name":"Barak","last_name":"Obama","position":"Co-Manager","office":"1505465460","start_date":"13/04/1985","salary":"4,081"},{"first_name":"Vincent","last_name":"Gambino","position":"Unemployed","office":"5313","start_date":"12/01/1980","salary":"4,081"}]

如上所述,页面加载成功后没有任何反应。 table 仅显示 headers 和一个 "Loading..." 文本,仅此而已。我现在有点迷路了,现在几乎什么都试过了。

有什么想法吗?

您的 JSON 与 the documentation 指定的格式不匹配。

{
  "data": [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "0,800"
    ],
    [
      "Garrett Winters",
      "Accountant",
      "Tokyo",
      "8422",
      "2011/07/25",
      "0,750"
    ],
    [
      "Ashton Cox",
      "Junior Technical Author",
      "San Francisco",
      "1562",
      "2009/01/12",
      ",000"
    ]
]
}

示例显示 JSON,根对象为 data,每一行都是它自己的数组。或者有一个 alternate format 对象,但它仍然需要一个 root 对象。修改您要返回的 JSON 以匹配其中一种预期格式。

顺便说一下,您使用的 WebMethodno longer supported. You should switch to ASP.NET Web API