【问题标题】:jquery.dataTables.min.js:48 Uncaught TypeError: Cannot read property 'length' of undefinedjquery.dataTables.min.js:48 未捕获类型错误:无法读取未定义的属性“长度”
【发布时间】:2016-09-24 13:15:25
【问题描述】:

我尝试在 DataTable 上使用 ajax,但出现此错误。 这里我解释一下我的代码:

脚本:

$(document).ready(function() {
        $('#tblEmployee').DataTable({
            "ajax": {
                url: '/Employee/GetEmployees',
                dataSrc: ''
            },
            "columns": [
                { '': 'sEmpName' },
                { '': 'sGender' }
            ]
        });
    });

JSON 返回:

[{"sEmpName":"Andi","sGender":"Man"},{"sEmpName":"Mark Will","sGender":"Women"},{"sEmpName":"Edward","sGender":"Man"}]

HTML:

<table id="tblEmployee" class="table table-bordered">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.sEmpName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.sGender)
                </th>                    
            </tr>
        </thead>

    </table>

我已经阅读了这个文档https://datatables.net/manual/ajax,但仍然出现错误..

【问题讨论】:

  • legacy.datatables.net/ref#sAjaxDataProp 参考这个 - 因为你的响应只是一个数组,你需要使用 sAjaxDataProp 来告诉 DataTables 这个事实 - 将它设置为一个空字符串(参见文档)。来源:datatables.net/forums/discussion/11901/…
  • 您能看看这个fiddle 并告诉我吗?
  • 感谢@Pat 的页面建议,它也很有效。我将为这篇文章创建一个答案.. :)
  • 啊抱歉@pat 我忘了.. XD 谢谢.. :)

标签: javascript jquery ajax asp.net-mvc datatables


【解决方案1】:

来自此来源的建议:https://datatables.net/forums/discussion/11901/uncaught-typeerror-cannot-read-property-length-of-undefined-jquery-datatables-js-1918

我这样改变了我的脚本:

$(document).ready(function() {
        $('#tblEmployee').DataTable({
            "sAjaxSource": '/Employee/GetEmployees',
            "sAjaxDataProp": "",
            "aoColumns": [
                 { "mDataProp": "sEmpName" },
                 { "mDataProp": "sGender" }
            ]
        });
    });

它也有效。谢谢你.. :)

注意: 我使用 jQuery 3.1.0 和 DataTables 1.10.12

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 2021-05-03
    • 2019-11-22
    • 2020-07-19
    相关资源
    最近更新 更多