Ajax 如何使用Json数据生成DataTable

Ajax 如何使用Json数据生成DataTable,ajax,datatables,Ajax,Datatables,我有API调用abd,它将返回Json,如下所示 { "status": true, "message": "Data Sent", "data": [ { "id": 9, "name": "North", "colType": 7, "userID": 0, "isVisible": false } ] }

我有API调用abd,它将返回Json,如下所示

{
    "status": true,
    "message": "Data Sent",
    "data": [
        {
            "id": 9,
            "name": "North",
            "colType": 7,
            "userID": 0,
            "isVisible": false
        }
    ]
}
下面我有一个JavaScript Ajax调用,但它返回的是空数据表,请提供帮助

$('#myTable').DataTable({
    destroy: true,
    responsive: true,
    "ajax": {
        "url": url+'/api/CommonMasters/7/GetByUserID/'+@ViewBag.UID,
        "type": "GET",
        "datatype": "json",
    },
    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
    "columns": [
        { "data": "name", "autoWidth": true },
        {
            "render": function (data, type, full, meta) { return "<a  id='Edit' class='btn btn-info' onclick='editRefUser(" + full.id + ")' ;>Edit</a>"; }
        },

    ]

});

$('#myTable')。数据表({
摧毁:没错,
回答:是的,
“ajax”:{
“url”:url+'/api/CommonMasters/7/GetByUserID/'+@ViewBag.UID,
“类型”:“获取”,
“数据类型”:“json”,
},
“长度菜单”:[[10,25,50,-1],[10,25,50,“全部”],
“栏目”:[
{“data”:“name”,“autoWidth”:true},
{
“呈现”:函数(数据、类型、完整、元){返回“编辑”;}
},
]
});

在ajax部分,您需要定义数据源:
“dataSrc”:“data”

为什么需要这样做?JSON数据结构的顶层没有“名称”项。名称嵌套在顶级“数据”对象中。这个额外的指令告诉DataTables只查看JSON的“数据”对象作为其起点

有关相关文档,请参阅:

ajax选项基本上继承了jQuery.ajax提供的所有选项,但我们提供了dataSrc的额外选项,以提供更改DataTables将从服务器返回的JSON中读取的数据的能力

另外,另一方面,我认为您不需要这个:
destroy:true
-尝试删除它。在初始化表对象时尝试销毁它可能没有意义

"ajax": {
    "url": url+'/api/CommonMasters/7/GetByUserID/'+@ViewBag.UID,
    "type": "GET",
    "datatype": "json",
    "dataSrc": "data"
},