【发布时间】:2017-07-30 18:40:23
【问题描述】:
我有一个 dot net aspx 页面,其中一个表设置为 JQuery DataTable (https://datatables.net/),使用 AJAX 调用从 Web 方法获取数据。数据作为 JSON 对象返回。
每个部分的机制似乎工作正常。调用 Web 方法并返回一个有效的 JSON 对象(根据 JSONLint),我可以在 AJAX 调用的“成功”回调函数中访问和处理返回的数据。
但是,数据表显示在页面上,没有任何正文行,只有页眉和页脚。
.aspx 页面中的 HTML 部分
<div id="divIssueDetailsTable">
<table id="tblIssueDetails" class="table">
<thead>
<tr>
<th>Team</th>
<th>Score</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Team</th>
<th>Score</th>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
</div>
创建数据表的Javascript函数(在成功回调函数中包含测试代码,用于测试返回的JSON对象)
$('#tblIssueDetails').DataTable(
{
"processing": true,
"serverSide": true,
"cache": false,
"destroy": true,
"ajax":
{
type: "POST",
url: "WebServices/WSGetData.asmx/ReturnIssues",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "",
success: function (results) {
toastr["success"]("AJAX call succeeded.", "Success:");
var objJSON = $.parseJSON(results.d);
$(objJSON.data).each(function () {
alert(this.Age);
});
},
error: function(e) {
toastr["error"]("There has been a problem retrieving the information from the database. " + e.responseText, "Error:");
},
complete: function () {
$('#divIssueDetailsTable').show();
}
}
});
网络方法返回的内容(测试数据)
d: "{ "数据": [{“姓名”:“弗雷迪”,“年龄”:“23”},{“姓名”:“简”,“年龄”:“23”},{“姓名”:“罗德”,“年龄”: “23”}] }"
奇怪的是,所有单独的部分似乎都工作正常,但只是没有刷新表格。
我在数据表上找不到任何链接,只是拒绝显示数据。我尝试了以下方法:
https://datatables.net/examples/basic_init/zero_configuration.html https://datatables.net/examples/data_sources/ajax.html fill datatable with json and web service Datatables not refreshing after second network request datatables dataSrc function not called on successful ajax response
非常感谢任何帮助。
【问题讨论】:
-
从 $(objJSON.data).each 中删除 .data 会发生什么?试试 $(objJSON).each
-
这是处理成功函数内部对象的测试代码。如果我删除“.data”,我将在警报框中得到“未定义”。问题是数据表没有显示返回的数据。谢谢。
标签: javascript jquery json ajax datatable