DataTables: Cannot read property 'length' of undefined DataTables: Cannot read property 'length' of undefined codeigniter codeigniter

DataTables: Cannot read property 'length' of undefined


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


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.


OK, thanks all for the help.

However the problem was much easier than that.

All I need to do is to fix my JSON to assign the array, to an attribute called data, as following.

{  "data": [{    "name_en": "hello",    "phone": "55555555",    "email": "a.shouman",    "facebook": "https:\/\/www.facebook.com"  }, ...]}