DataTables: Cannot read property 'length' of undefined

As there are 4 columns, add the following in "aoColumns":

"aoColumns": [
  { "mData": null },  // for User Detail
  { "mData": "LoginId" },
  { "mData": "Name" },
  { "mData": "CreatedDate" }
]

For undefined length, I have tried the following code and it's working:

$('#example').dataTable({
 "aLengthMenu": [[100, 200, 300], [100, 200, 300]],
  "iDisplayLength": 100,
  "iDisplayStart" : 0,
  "bProcessing": true,
  "bServerSide": true,
  "sAjaxSource": "GetUser.ashx",
  "sServerMethod": "POST",
  "sAjaxDataProp" : "",
  "aoColumnDefs": [ {
    "aTargets": [ 0 ],
    "mData": "download_link",
    "mRender": function ( data, type, full ) {
      return '<a href="/UserDetail.aspx?ID='+data+'">Detail</a>';
    }
  } ],
  "aoColumns": [
    { "mData": null },
    { "mData": "LoginId" },
    { "mData": "Name" },
    { "mData": "CreatedDate" }
  ]
});

The reference site to know more about aLengthMenu is:

https://legacy.datatables.net/ref#aLengthMenu


If you see this error, look at the json returned from the server, and then make sure that all of your datatable 'mData' values have matching entry. If you are also using a bean, check there as well.

And there is no need specify 'tr', 'td', etc for the table. It is much cleaner if you let jquery do that for you. Here is what my tables look like:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="myTable" style="table-layout:fixed" ></table>

and then my datatable elements specify the width and column title:

{sTitle: "Column A", sWidth: "100px", mData: "idStringFromJson", bSortable: false},

Your "sAjaxDataProp" : "" is set to an empty string, which causes this error.

dataTables expects to have a string here to tell under which key your server returned data can be found. This defaults to aaData, so your json should include this key amongst all others that might be returned or needed by pagination etc.

Normal serversided json:

{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"aaData": [
    [
        "1",
        "sameek",
        "sam",
        "sam",
        "[email protected]",
        "1",
        ""
    ],...

Since all values are in aaData you don't need sAjaxDataProp at all.

Modified serverside json:

{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"myData": [
    [
        "1",
        "sameek",
        "sam",
        "sam",
        "[email protected]",
        "1",
        ""
    ],

Now the values are in myData. so you need to tell dataTables where to find the actual data by setting:

"sAjaxDataProp" : "myData"

Here is a Plunker