jquery.dataTables.min.js:48未捕获的TypeError:无法读取未定义的属性'length'

时间:2016-09-24 13:15:26

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

我尝试在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,但仍然收到错误..

1 个答案:

答案 0 :(得分:2)

来自此来源的建议: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