URGENT - Basic Ajax Question

URGENT - Basic Ajax Question

GuzuGuzu Posts: 1Questions: 0Answers: 0

I have a json response as shown below and when I tie it to the datatable I am getting error "Cannot read property 'length' of undefined" I know DataTable is looking for "Data" field but I am not able to change the source. Any ideas greatly appreciated. Here is the basic code:

$(document).ready(function () { $('#EventLog').DataTable({ "destroy": true, "ajax": "../../api/logs/getlog" }); });

And Data is:

[{"EventDate":"2016-11-02T09:06:00","Message":"upgrade libisc-export160:amd64 1:9.10.3.dfsg.P4-8ubuntu1.1 1:9.10.3.dfsg.P4-8ubuntu1.2","ID":1,"Severity":"High","System":"Mysql"},{"EventDate":"2016-11-02T09:06:00","Message":"status triggers-pending libc-bin:amd64 2.23-0ubuntu3","ID":2,"Severity":"Information","System":"Apache"}]

Replies

  • PaulusPaulus Posts: 69Questions: 17Answers: 5

    This will not work, DataTable expects a structure like this...

    {
       "draw":null,
       "data":[
           {
              "EventDate":"2016-11-02T09:06:00",
              "Message":"upgrade libisc-export160:amd64 1:9.10.3.dfsg.P4-8ubuntu1.1 1:9.10.3.dfsg.P4-8ubuntu1.2",
              "ID":1,
              "Severity":"High",
              "System":"Mysql"
           },
           {
              "EventDate":"2016-11-02T09:06:00",
              "Message":"status triggers-pending libc-bin:amd64 2.23-0ubuntu3",
              "ID":2,
              "Severity":"Information",
              "System":"Apache"
           }
       ],
       "recordsTotal":2,
       "recordsFiltered":2,
       "error":null,
       "fieldErrors":[
    
       ],
       "id":null,
       "options":{
    
       },
       "cancelled":[
    
       ]
    }
    
  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin

    Actually, unless you are using server-side processing (init serverSide), yes, that data structure is acceptable to DataTables using the ajax.dataSrc option set to be an empty string (telling DataTables to expect a flat array rather than looking inside an object property for an array).

    There is an example of that available here.

    Allan

This discussion has been closed.