【发布时间】:2014-08-31 21:44:49
【问题描述】:
我正在开发一个ASP.NET Web Forms 应用程序。使用Visual Studio 2012 标准模板。我想将DataTable 1.10 与对服务器的Ajax 调用一起使用。由于我在使用datatables 时遇到了麻烦,所以我从头开始,所以我可以确认当我使用更简单的示例(如从 txt 文件中读取)时它工作正常。但是现在,按照官方网站的例子,我想实现真正的Ajax调用一个方法并返回一些数据。
我的页面Test.aspx 有此代码:
<script type="text/javascript">
$(document).ready(function () {
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": "/Table.aspx/TestAjax"
});
});
还有一个非常简单的 HTML:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
</tr>
</tfoot>
</table>
所有这些都取自示例,为了简单起见,还最小化了列数。
从一个示例中,我将这段代码用于我的方法:
[WebMethod]
public static string TestAjax(string sEcho)
{
var list = new FormatedList();
list.sEcho = sEcho;
list.iTotalRecords = 1;
list.iTotalDisplayRecords = 1;
var item = new List<string>();
item.Add("Gecko");
list.aaData = new List<List<string>>();
list.aaData.Add(item);
return list.ToString();
}
其中FormatedList 是这样定义的类:
public class FormatedList
{
public FormatedList()
{
}
public string sEcho { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public List<List<string>> aaData { get; set; }
}
因此,当我在控制台中运行我的项目时,我可以看到 Status 200 OK 调用了 Test.aspx/TestAjax,但我也收到了来自浏览器的错误 Json 无效。在服务器端代码的示例中,方法的返回类型是实际的类FormatedList,但它给出了相同的结果。我可以理解,在这两种情况下,我都没有返回实际的 Json 浏览器给我一个错误,但是,我在示例中看到人们正在使用这样的方法,听起来它正在工作。
那么我的错误在哪里?
【问题讨论】:
标签: asp.net ajax webforms datatable