【发布时间】:2018-01-19 03:24:53
【问题描述】:
我知道这是一个受欢迎的问题。我发布我的问题是因为我试图在 SO 上与其他人调查我的问题但无济于事。
------我使用的是旧的 jquery 数据表------ ---
我看到了不一致的行为。我有两个数据表,除了每个都与不同的控制器通信外,它们在各方面都是相同的。一个完美填充,另一个在 jQuery 代码中给了我这个错误。
我正在获取 json 数据,但 _fnGetObjectDataFn( oSettings.sAjaxDataProp )(json) 将 aData 设置为未定义! 说什么?这是json数据:
为了确保问题不在于列数不正确,我已将表格最小化为只有一列。这是弹出表视图的 javaScript:
initBuildingsTable = function () {
$('#selectableAssetTable').dataTable({
"bPaginate": false,
"bProcessing": true,
"bAutoWidth": true,
"aoColumns": [
{ "mDataProp": "SiteName", "bSortable": true },
],
"aoColumnDefs": [
{ "mDataProp": null, "sDefaultContent": " ", "aTargets": [-1] }
],
"sDom": '<"top">rt<"bottom"><"clear">',
"oLanguage": {
"sEmptyTable": "No data found."
},
"sAjaxSource": $.baseURL("api/selfservice/getsites"),
"fnServerData": function (sSource, aoData, fnCallback) {
$.ajax({
url: sSource, // Do not add the base to this. It should already be present
type: "GET",
dataType: "json",
success: fnCallback,
complete: function () {
alert("show me the data!")
}
});
}
});
}
这是表格的 HTML:
<table id="selectableAssetTable" style="width: 100%; ">
<thead>
<tr>
<th>Site Name</th>
</tr>
</thead>
<tbody></tbody>
</table>
这是从控制器返回的数据的 DTO:
public class SiteDTO
{
public decimal SiteId { get; set; }
public string SiteName { get; set; }
public string Address { get; set; }
public string Contact { get; set; }
}
【问题讨论】:
-
首先检查
api/selfservice/getsitesapi是否返回有效的json..stackoverflow.com/a/22470507/5737771 -
_fnGetObjectDataFn(oSettings.sAjaxDataProp)应该是什么?
-
@Jonasw - 它应该是一个数组[362]
-
@patricia 所以如果你用 (json) 调用它可能会引发错误? ://
-
@Jonasw - 我在上面的评论中错了!我应该返回一个包含 aaData 作为数组的对象。我希望这对其他人有帮助。感谢您帮助我发现差异。
标签: javascript jquery json datatables