jquery – DataTables:无法读取undefined的属性’length’

我理解这是一个受欢迎的问题,我已经在Stack Overflow和其他网站(包括datatables网站)上阅读了所有类似的问题.

为了澄清,我正在使用

> PHP Codeigniter
> Materliazecss

我还确保我正确地收到了JSON数组:

[{"name_en":"hello","phone":"55555555","email":"a.shouman","facebook":"https:\/\/www.facebook.com"},{"name_en":"Mahfouz","phone":"00000000","email":"m.mahfouz","facebook":null},{"name_en":"Abdelrahman",{"name_en":"ayman test","phone":"55554444",{"name_en":"Abdelrahman Mossad Shouman Bootstrap","phone":"12345678","email":"a.shouman@mail.com","facebook":"https:\/\/www.facebook.co"},{"name_en":"a.sharara","email":"a.sharara","facebook":""}]

我的HTML表格如下所示:

<table id="customer_table">
     <thead>
         <tr>
            <th>Name</th>
            <th>Contact</th>
            <th>Email</th>
            <th>Facebook</th>
         </tr>
     </thead>
</table>

这是我的document.ready功能

$(document).ready(function(){
            //$('#customer_table').DataTable();
            $('#customer_table').DataTable( {
                "ajax": 'json',"dataSrc": "","columns": [
                    { "data": "email" },{ "data": "facebook" },{ "data": "name_en" },{ "data": "phone" }
                ]
            });
  });

我得到的错误

Uncaught TypeError: Cannot read property ‘length’ of undefined

解决方法

它甚至更简单:juste在ajax定义中使用dataSrc:”选项,因此dataTable不期望一个对象而是一个数组:
$('#pos-table2').DataTable({
                "processing": true,"serverSide": true,"ajax":{"url":"pos.json","dataSrc":""}
            }
    );

ajax options

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...