我已经遇到这个问题一段时间了,每次都遇到不同的错误或某种无助于我实现目标的问题。
我正在使用 jQuery 的表插件,但我似乎无法从我那里得到适合表数据的 WebMethod ASP.NET 正确的 Json 响应。我的表的状态卡在"正在加载...."上,似乎什么也没发生,即使我确实从我的 WebMethod 收到了正确的 Json 响应。它似乎不会影响我的 dataTable,或者只是在初始化期间由于某些问题而冻结它。
我的代码:
客户端:
<%@ 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.4extensionsScrollerjsdataTables.scroller.js"></script>
<script>
$('#example').dataTable({
//"ajax": "ajaxtest.txt",
"ajax": {
"url": "WebService.asmx/GetTable",
"type": "POST"
},
"deferRender": true
});
</script>
</body>
</html>
My 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":"$124,081"},{"first_name":"David","last_name":"Naidich","position":"Worker","office":"052-343-4120","start_date":"12/11/1990","salary":"$124,081"},{"first_name":"Barak","last_name":"Obama","position":"Co-Manager","office":"1505465460","start_date":"13/04/1985","salary":"$124,081"},{"first_name":"Vincent","last_name":"Gambino","position":"Unemployed","office":"5313","start_date":"12/01/1980","salary":"$124,081"}]
如上所述,页面成功加载后不会发生任何操作。该表仅显示标题和"正在加载..."文本,仅此而已。我现在有点迷茫,现在几乎尝试了任何事情。
有什么想法吗?
您的 JSON 与文档指定的格式不匹配。
{
"data": [
[
"Tiger Nixon",
"System Architect",
"Edinburgh",
"5421",
"2011/04/25",
"$320,800"
],
[
"Garrett Winters",
"Accountant",
"Tokyo",
"8422",
"2011/07/25",
"$170,750"
],
[
"Ashton Cox",
"Junior Technical Author",
"San Francisco",
"1562",
"2009/01/12",
"$86,000"
]
]
}
该示例显示了根对象为 data
的 JSON,每行都是它自己的数组。或者有一种带有对象的替代格式,但它仍然需要一个root
对象。修改要返回的 JSON 以匹配预期的格式之一。
顺便说一下,您正在使用不再受支持的WebMethod
。应切换到 ASP.NET Web API。