Can't read JSON without header

Can't read JSON without header

trusta77trusta77 Posts: 4Questions: 2Answers: 0

So I'm trying to do a row detail, using data from JSON following this Row Details

In that example, the jason have a header (I don't know what to call it, I'm relatively new to JSON and AJAX) like this :

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [

While my JSON from json_encode array doesn't have this, it goes directly to data like this:

[{
      "DT_RowId": "row_5",
      "first_name": "Airi",
      "last_name": "Satou",
      "position": "Accountant",
      "office": "Tokyo",
      "start_date": "28th Nov 08",
      "salary": "$162,700"
}]

Is there any way to parse the data without using the header or to set the header to my JSON?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    That header is only needed when serverSide is enabled - if it's not, or if it's set to false then the header isn't required, only the second data that you pasted. What do you have in your initialisation?

    Colin

  • trusta77trusta77 Posts: 4Questions: 2Answers: 0

    Previously my serverSide is true, but even after set it to false it's still show error

    jquery.dataTables.min.js:48 Uncaught TypeError: Cannot read property 'length' of undefined

    This is my istallation look like:

    var dt = $('#datatable').DataTable( {
            "order": [[ 0, "asc" ]],
            "lengthMenu": [50, 25, 10],
            "processing": false,
            "serverSide": false,
            "ajax": "ajax.php",
            "type":"post",
            "columns": [
            {
              "class":"details-control",
              "orderable":false,
              "data":null,
              "defaultContent": ""
            },
            { "data": "first_name" },
            { "data": "last_name" },
            { "data": "position" },
            { "data": "office" }
            ],
            "order": [[1, 'asc']]
          } );
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Change:

    "ajax": "ajax.php"
    

    to be

    ajax: {
       url: 'ajax.php',
       dataSrc: ''
    }
    

    The data is at the top level of the returned object - the default would be 'data',

    Colin

  • trusta77trusta77 Posts: 4Questions: 2Answers: 0

    Oh, it's working nicely.
    In my previous attempt, I use javascript to parse my ajax and then using row.add() to add data to the table in a loop, this one could actually save lots of time. Thankyou very muuch.

This discussion has been closed.