【问题标题】:DataTables: Uncaught TypeError: Cannot read property 'error' of undefined数据表:未捕获的类型错误:无法读取未定义的属性“错误”
【发布时间】:2016-11-21 22:41:03
【问题描述】:

这是我的 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()函数内部,如下:

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

【问题讨论】:

  • 你在成功调用中尝试过console.log on json吗?
  • 当我运行这个 -- 'ajax': { 'url': '/my/data/path/src.php', 'dataSrc': 'data', 'success': function(json){ console.log(json); } } 它说undefined
  • 文档没有告诉我我期望看到的成功...您可以尝试使用“完成”替换“成功”吗?
  • 同样的错误:'ajax': { 'url': '/my/data/path/src.php', 'dataSrc': 'data', 'done': function(json){ console.log(json); }
  • @DaveyJake 请试试这个。在您的成功处理程序中:“console.log(arguments);”这将返回所有传递给的参数,即使它们不在函数定义中。

标签: php json datatables


【解决方案1】:

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

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

【讨论】:

  • 同样的错误。我的 src.php 文件如下所示:{ "draw": 1, "recordsTotal": 3, "recordsFiltered": 3, "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" }] }
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 2017-02-11
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多