DataTables:Uncaught TypeError:无法读取属性'错误'未定义的

时间:2016-11-21 22:41:03

标签: php json datatables

这是我的HTML:

<table id="mens-clubs" class="display" cellspacing="0" role="grid" data-page-length="50">
    <thead>
        <tr>
            <th width="0">ID</th>
            <th width="20%">Club</th>
            <th width="20%">City</th>
            <th width="20%">State</th>
            <th width="20%">Metro</th>
            <th width="20%">Union</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th width="0">ID</th>
            <th width="20%">Club</th>
            <th width="20%">City</th>
            <th width="20%">State</th>
            <th width="20%">Metro</th>
            <th width="20%">Union</th>
        </tr>
    </tfoot>
    <tbody></tbody>
</table>

这是我的JS:

$(document).ready(function() {
    $('#mens-clubs').DataTable({
        'ajax': {
            'url': '/my/data/path/src.php',
            'dataSrc': 'data'
        },
        'columns': [
            { 'data': 'id' },
            { 'data': 'clubName' },
            { 'data': 'city' },
            { 'data': 'state' },
            { 'data': 'metroArea' },
            { 'data': 'unionName' }
        ],
        'processing': true,
        'dom': '<"row"<"small-24 column"B>><"row"<"large-8 columns"<"row filter-wrapper"<"columns small-24"f>>><"large-16 columns right"p>>' + 't' + '<"row"<"small-24 columns"p>>',
        'buttons': [ 'copy', 'excel', 'csv', 'pdf', 'print' ],
        'searching': true,
        'language': {
            'search': '',
            'searchPlaceholder': 'Search Clubs'
        },
        'pagingType': 'full_numbers',
        'scrollY': '50vh',
        'scrollCollapse': true,
        'deferRender': true
    });
});

以下是我的src.php文件的内容:

{
  "data": [{
    "id": "3",
    "clubName": "Alpha Steelers",
    "city": "Alpha",
    "state": "UT",
    "metroArea": "",
    "unionName": "Rocky Mountain"
  }, {
    "id": "5",
    "clubName": "Beta of Aspen",
    "city": "Beta",
    "state": "CO",
    "metroArea": "",
    "unionName": "Rocky Mountain"
  }, {
    "id": "6",
    "clubName": "Gamma Highlanders",
    "city": "Gamma",
    "state": "CO",
    "metroArea": "",
    "unionName": "Rocky Mountain"
  }]
}

我每次都会通过GET成功检索到我的数据文件,不过,我在成功检索后立即收到此错误:

Uncaught TypeError: Cannot read property 'error' of undefined

DataTables.js中引用的错误行是79406。它位于_fnBuildAjax()函数内部,如下所示:

enter image description here

有没有人遇到过这个错误或者想知道这个错误是指什么?我通过src.php运行了我的JSONLinter文件并且该文件有效。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的json Feed缺少一些信息。 Jquery将发送一个“抽奖”号码,需要反馈。您需要总数和过滤数据以及数据。

$complete = array("draw" => $this->request->query['draw'],
            'recordsTotal' => $total,
            "recordsFiltered" => $filtered,
            "data" => $output);