我已经暂时停留在这个问题上一段时间,每次都会遇到不同的错误或某种不能帮助我实现目标的问题。
我正在使用jQuery的表插件,我似乎无法从ASP.NET WebMethod获得适当的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.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":"$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"}]
如上所述,一旦页面成功加载,就不会发生任何事情。该表只显示标题和&#34;正在加载......&#34;文本,以及它的相关内容。我现在有点迷失了,现在已经尝试了很多东西。
有什么想法吗?
答案 0 :(得分:3)
您的JSON与the documentation指定的格式不匹配。
{
"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,每行都是自己的数组。或者有alternate format个对象,但它仍然需要root
个对象。修改您返回的JSON以匹配其中一种预期格式。
顺便说一下,您使用的WebMethod
是no longer supported。您应该切换到ASP.NET Web API。