javascript - dataTable 给出未捕获的类型错误 : Cannot read property 'mData' of undefined

标签 javascript jquery datatables

我正在尝试使用 jQuery Datatable 插件在 HTML 页面中进行分页。

<!DOCTYPE html>
<html>
<head> 
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript"  src=" https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"  src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>

<meta charset="UTF-8">
<title>Clients</title>
</head>
<body>
<table style="width:100%" id="clients_data">
<caption>Clients</caption>
  <tr>
    <th>Clients</th>
    <th>Number of Sites</th> 
    <th>Reset the Processing</th> 
  </tr>
  </table>

<table style="width:100%" id="machines_data">
<caption>Machines</caption>
  <tr>
   <th>Number</th>
    <th>Machine Name</th>
  </tr>
  </table>

$(document).ready(function() {
     loadCustomers();
     loadMachines();
    $('#clients_data').DataTable( {
        "pagingType": "full_numbers"
    } );
    $('#machines_data').DataTable( {
        "pagingType": "full_numbers"
    } );

} );


function loadCustomers() {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8080/cache/getCustomers',
        dataType: 'json',
        success: function(data) {
            var rows = [];    
            $.each(data,function(id,value) {
                      rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset(\''+id+'\')">Reset</td></tr>');
                    });
            $('#clients_data').append(rows.join(''));
        }
    });
};

function loadMachines() {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8080/cache/getMachines',
        dataType: 'json',
        success: function(data) {
             var rows = [];    
             $.each(data,function(id,value) {
                      rows.push('<tr><td>'+id+'</td><td><a href="machineInfo.html?machine='+value+'">'+value+'</td></tr>');
                    });
             $('#machines_data').append(rows.join(''));

        }
    });
};
</script>
</body>
</html>

对于上述内容,当我尝试加载页面时,我得到:

Uncaught TypeError: Cannot read property 'mData' of undefined at $('#clients_data').DataTable({...})

我的脚本有什么问题? 我正在关注this guide .

我的 fiddle : https://jsfiddle.net/yeu7f3f2/

最佳答案

将两个 $.datatable() 函数调用移动到每个调用的成功函数内。

请记住,ajax 是异步的,这意味着即使它尚未返回,下一行也会在调用它后立即执行。我们希望确保仅当 ajax 完成插入表数据时才调用 datatable() 函数。

关于javascript - dataTable 给出未捕获的类型错误 : Cannot read property 'mData' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763366/

相关文章:

javascript - 在 Dropzonejs 中调用 .processQueue() 时出错

javascript - 测试从某些 JavaScript 返回 JSON 的 ASP.NET Web 服务的最简单方法?

javascript - Bootstrap3 限制选择和淡入淡出未选中

javascript - 可能的逻辑错误或比较器问题

javascript - 按特定顺序观看和编译多个文件夹中的 CoffeeScript 和 vendor javascript 文件,还组织 sass 和 html 模板

javascript - 根据键将一个数组嵌套到另一个数组中

javascript - Datatables 服务器端 ajax 和 sAjaxSource 具有不同的请求参数

javascript - DataTable Editor Dependent() - 根据第一个选定的值仅显示/隐藏某些值

jquery - Asp.net 文件上传图片预览

vue.js - Vuetify 可扩展数据表 : Expand row position