table 中没有可用数据 - 数据tables jquery 问题
No data available in table - datatables jquery issue
我有一个 jquery-datatables 是从 C# 中的方法填充的。
我已验证的 C# 方法 returns 一些 json。但是我的列永远不会被填充,而是我收到一条消息说 No data available in table.
我完全卡住了,希望有人能帮忙!
var table = $('#example').DataTable({
ajax: {
url: pageUrl,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
dataSrc: function (data) {
console.log(data.d);
return $.parseJSON(data.d);
}
},
"pageLength": 50,
fixedHeader: true,
responsive: true,
"columns": [
{
"data": "key",
"render": function (data, type, row, meta) {
if (type === 'display') {
data = '<a href="/software/details.aspx?id=' + data + '">' + data + '</a>';
}
return data;
} },
{ "data": "summary" },
{ "data": "created" },
{ "data": "updated" },
{ "data": "Status" },
{ "data": "priority" },
{ "data": "reporter" },
{ "data": "assignee" }
],
autoWidth: false,
"columnDefs": [
{ "width": "50%", "targets": 0 },
{ "width": "5%", "targets": 1 },
{ "width": "5%", "targets": 2 },
{ "width": "5%", "targets": 3 },
{ "width": "5%", "targets": 4 },
{ "width": "5%", "targets": 5 }
],
"order": [[1, 'asc']],
"success": fnsuccesscallback,
"error": fnerrorcallback
});
function fnsuccesscallback(data) {
alert(data.d);
}
function fnerrorcallback(result) {
alert(result.statusText);
}
这是 console.log 输出中的数据:
{"summary":"External Change Request Form: tester - 19/1/2021","created":"2021-01-19T15:32:02+00:00","updated":"2021-06-03T08:59:17+01:00","status":"To Do","key":"ITS-4711","assignee":"Bob","priority":"Low","reporter":"Dave"}
问题是这部分:
dataType: "json",
dataSrc: function (data) {
console.log(data.d);
return $.parseJSON(data.d);
}
在 dataSrc
回调中,data
已经是一个 javascript 对象,而不是 JSON 字符串。调用 $.parseJSON(object)
会产生一个错误,看起来数据表正在捕捉(并忽略,这是不好的做法)。
因此给出了一个不相关的错误消息,即没有数据,而不是数据处理失败。
将return $.parseJSON(data.d);
改为
return data.d;
我有一个 jquery-datatables 是从 C# 中的方法填充的。
我已验证的 C# 方法 returns 一些 json。但是我的列永远不会被填充,而是我收到一条消息说 No data available in table.
我完全卡住了,希望有人能帮忙!
var table = $('#example').DataTable({
ajax: {
url: pageUrl,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
dataSrc: function (data) {
console.log(data.d);
return $.parseJSON(data.d);
}
},
"pageLength": 50,
fixedHeader: true,
responsive: true,
"columns": [
{
"data": "key",
"render": function (data, type, row, meta) {
if (type === 'display') {
data = '<a href="/software/details.aspx?id=' + data + '">' + data + '</a>';
}
return data;
} },
{ "data": "summary" },
{ "data": "created" },
{ "data": "updated" },
{ "data": "Status" },
{ "data": "priority" },
{ "data": "reporter" },
{ "data": "assignee" }
],
autoWidth: false,
"columnDefs": [
{ "width": "50%", "targets": 0 },
{ "width": "5%", "targets": 1 },
{ "width": "5%", "targets": 2 },
{ "width": "5%", "targets": 3 },
{ "width": "5%", "targets": 4 },
{ "width": "5%", "targets": 5 }
],
"order": [[1, 'asc']],
"success": fnsuccesscallback,
"error": fnerrorcallback
});
function fnsuccesscallback(data) {
alert(data.d);
}
function fnerrorcallback(result) {
alert(result.statusText);
}
这是 console.log 输出中的数据:
{"summary":"External Change Request Form: tester - 19/1/2021","created":"2021-01-19T15:32:02+00:00","updated":"2021-06-03T08:59:17+01:00","status":"To Do","key":"ITS-4711","assignee":"Bob","priority":"Low","reporter":"Dave"}
问题是这部分:
dataType: "json",
dataSrc: function (data) {
console.log(data.d);
return $.parseJSON(data.d);
}
在 dataSrc
回调中,data
已经是一个 javascript 对象,而不是 JSON 字符串。调用 $.parseJSON(object)
会产生一个错误,看起来数据表正在捕捉(并忽略,这是不好的做法)。
因此给出了一个不相关的错误消息,即没有数据,而不是数据处理失败。
将return $.parseJSON(data.d);
改为
return data.d;