Javascript DataTables-未捕获类型错误:无法读取属性';长度';未定义的
我已经看到了这个问题的几个例子,但仍然没有找到解决方案 错误表示它在jquery.dataTables.js(版本1.10.4)的第3287行中断,如下所示Javascript DataTables-未捕获类型错误:无法读取属性';长度';未定义的,javascript,json,datatables,Javascript,Json,Datatables,我已经看到了这个问题的几个例子,但仍然没有找到解决方案 错误表示它在jquery.dataTables.js(版本1.10.4)的第3287行中断,如下所示 // Got the data - add it to the table for ( i=0 ; i<aData.length ; i++ ) { _fnAddData( settings, aData[i] ); } 我的javascript $(document).ready(function(
// Got the data - add it to the table
for ( i=0 ; i<aData.length ; i++ ) {
_fnAddData( settings, aData[i] );
}
我的javascript
$(document).ready(function() {
$('#directory_table').dataTable( {
"ajax": {
"url": "test",
"type": "JSON"
},
"aoColumns": [
{ "persons": "preferred_name" },
{ "persons": "last_name" },
{ "persons": "phone_numbers.0" },
{ "persons": "phone_numbers.1" },
{ "persons": "phone_numbers.2" },
{ "persons": "email" },
{ "persons": "department" },
{ "persons": "title" }
]
} );
} );
我的HTML
<table id='directory_table' class="display">
<thead>
<tr style='background: #186A9F; color: white;'>
<th>First Name </th>
<th>Last Name</th>
<th>Desk Number</th>
<th>Mobile</th>
<th>Branch</th>
<th>Email</th>
<th>Department</th>
<th>Title</th>
</tr>
<thead>
</table>
名字
姓
办公桌号码
可移动的
分支机构
电子邮件
部门
标题
原因
默认情况下,DataTables期望JSON响应具有特定的结构,请参阅
数组的数组:
{
“数据”:[
[“值1”、“值2”],
...
]
}
对象数组:
{
“数据”:[
{
“属性1”:“值1”,
“属性2”:“值2”
},
...
]
}
发生错误的原因是响应中包含对象数组(persons
)的数据属性的名称与默认值(data
)不同
解决方案
使用选项定义要读取的数据源对象(即Ajax请求返回的对象)的属性
$('#目录_表')。数据表({
“ajax”:{
“url”:“测试”,
“dataSrc”:“人”
},
“栏目”:[
{“数据”:“首选名称”},
{“数据”:“姓氏”},
{“数据”:“电话号码.0.desk”},
{“数据”:“电话号码.1.手机”},
{“数据”:“电话号码.2.分支”},
{“数据”:“电子邮件”},
{“数据”:“部门”},
{“数据”:“标题”}
]
});
或者,如果您可以将JSON响应中的数据属性名称从persons
更改为data
,则不需要添加“dataSrc”:“persons”
演示
有关代码和演示,请参阅
链接
有关此错误和其他常见控制台错误的更多信息,请参阅。yes URL在我这边是正确的。我能够运行ajax,点击我的控制器,我的json通过DataTables javascript传递。我一有机会就试试这个。谢谢你的反馈。
<table id='directory_table' class="display">
<thead>
<tr style='background: #186A9F; color: white;'>
<th>First Name </th>
<th>Last Name</th>
<th>Desk Number</th>
<th>Mobile</th>
<th>Branch</th>
<th>Email</th>
<th>Department</th>
<th>Title</th>
</tr>
<thead>
</table>