【问题标题】:No data available in table - datatables jquery issue表中没有可用数据 - 数据表 jquery 问题
【发布时间】:2021-06-09 09:50:29
【问题描述】:

我有一个 ,我是从 C# 中的一个方法填充的。

C# 方法返回一些我已经验证过的 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"}

【问题讨论】:

  • 看起来 data.d 已经是一个 javascript 对象,而不是 JSON 字符串,尤其是 dataType:'json' - 尝试将 return $.parseJSON(data.d); 更改为 return data.d
  • @freedomn-m 对它进行了排序,谢谢!如果您想将其作为答案,我可以将其标记为正确吗? :)

标签: jquery-datatables jquery datatable datatables


【解决方案1】:

问题是这部分:

        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;

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 2015-11-27
    • 2015-12-27
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多