DataTables: Cannot read property 'length' of undefined
DataTables: Cannot read property 'length' of undefined
回答1
It's even simpler: just use dataSrc:''
option in the ajax defintion so dataTable knows to expect an array instead of an object:
$('#pos-table2').DataTable({
processing: true,
serverSide: true,
ajax:{url:"pos.json",dataSrc:""}
}
);
See ajax options
回答2
CAUSE
This errors TypeError: Cannot read property 'length' of undefined
usually means that jQuery DataTables cannot find the data in the response to the Ajax request.
By default jQuery DataTables expects the data to be in one of the formats shown below. Error occurs because data is returned in the format other than default.
Array of arrays
{
"data": [
[
"Tiger Nixon",
"System Architect",
"$320,800",
"2011/04/25",
"Edinburgh",
"5421"
]
]
}
Array of objects
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
}
]
}
SOLUTION
Use default format or use ajax.dataSrc
option to define data property containing table data in Ajax response (data
by default).
See Data array location for more information.
LINKS
See jQuery DataTables: Common JavaScript console errors for more details.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-06-08 二元一次方程求解
2021-06-08 Retargeting All Projects in a Solution to .NET 4.5.2
2021-06-08 IIS Tunning Recommendations against Slow http Attacks
2018-06-08 GeHost powershell
2017-06-08 The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.