【发布时间】:2013-04-14 08:13:49
【问题描述】:
我可以像下面这样与 DataTables.net 进行静态绑定:
<script type="text/javascript">
$(document).ready(function () {
$('#testDatatable').dataTable({
"aaData": [
["Ibrahim", 55],
["Asif", 20],
["Shariful", 70],
["John", 55],
["Doe", 40],
["Nazmul", 30],
["Jane", 15],
["Ershad", 10],
["Yusuf", 44],
["Bill", 22],
["Steve", 18]
]
,
"aoColumns": [
{ "sTitle": "Name" },
{ "sTitle": "Age" }
]
});
});
</script>
但是对于 prop aaData,我想从 Web 服务中获取数据。如下:
<script type="text/javascript">
$(document).ready(function () {
$('#testDatatable').dataTable({
"aaData": $.getJSON('http://localhost:9183/Service.svc/GetCustomer')
,
"aoColumns": [
{ "sTitle": "Name" },
{ "sTitle": "Age" }
]
});
});
</script>
我的服务如下所示:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public List<Customer> GetCustomer()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Name = "Ibrahim", Age = 10 });
customers.Add(new Customer { Name = "John Doe", Age = 20});
return customers;
}
这不起作用。它表明没有找到任何记录。 有人请告诉我我有哪些选择或哪里出错了。 任何建议都非常感谢。
【问题讨论】:
-
你试过json类型吗?
-
[WebGet(ResponseFormat = WebMessageFormat.Json)]应该将列表作为 JSON 返回,那么为什么我应该明确地将数据作为 JSON 字符串返回? -
但如果没有 JSON 字符串,您将无法获取单个元素/属性,例如 id、name 等...我想您应该仔细阅读文档
-
没有文档,该服务是我自己的以及客户的。数据以 JSON 形式返回给我,或者我只能将其剥离为 JSON。但是 DataTables 没有响应我绑定的数据源。问题出在哪里。
标签: jquery wcf datatables