jquery datables with php json_encode cannot work?

jquery datables with php json_encode cannot work?

matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0
edited June 2016 in Free community support

i try to get curl data to jquery data table (like this https://datatables.net/examples/api/row_details.html),

Here is the response.php

echo json_encode($results); 

the json_encode output:

{
 "hittotal":69511,
 "data":[
   {
       "message":"#, @varbind_list=[#, #, #, #], @specific_trap=6,@source_ip=\"1.2.3.4\", @agent_addr=#, @generic=6>",
       "@timestamp":"2016-06-21T02:29:47.455Z",
        "datetime":"2016-06-21T10:29:47.455Z",
       "type":"trap","datetime":"2016-06-21T10:29:47.455Z",
        SENTRY2_MIB::sentry2ChainLocation":"LA",
        "Sentry2BoardId":"D","sentry2PortPowerAction":"2",
       "SENTRY2_MIB::sentry2PortDeviceName":"x3",
       "SENTRY2_MIB::sentry2PortModuleStatus":"Off Fail",
        "ip":"2.3.4.5",
        "enterprise":"1.3.6.1.4.1.1718.2.100",
        "id":"1.3.6.1.4.1.1718.2.100.0.6"
       },.....
     ]
}

<br>
HTML table and javascript in my web page(datatables.php):

<script>
function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="3" cellspacing="0" border="0" style="padding-left:50px;">'+
    '<tr>'+
        '<td>message:</td>'+
        '<td>'+htmlspecialchars(d.message)+'</td>'+
    '</tr>'+
    '<tr>'+
        '<td>id:</td>'+
        '<td>'+d.id+'</td>'+
    '</tr>'+

'</table>';
}

$(document).ready(function() {
var table = $('#example').DataTable( {
      "processing": true,
      "serverSide": true,
    "ajax": {
    'type': 'POST',
    'url': 'response.php',
    'data': {
       from: '<? echo $from; ?>',
       to: '<? echo $to; ?>',
       device:'<? echo $dev; ?>',
       keyword:'<? echo $keyword; ?>',
       index:'<? echo $index; ?>'

    },
    "columns": [
        {
            "className":      'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": ''
        },
        { "data": "datetime" },
        { "data": "message" },

    ],
    "order": [[1, 'asc']]
} );

// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
    var tr = $(this).closest('tr');
    var row = table.row( tr );

    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child( format(row.data()) ).show();
        tr.addClass('shown');
     }
  } );
 } );
</script>

html :

<body>

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th>datetime</th>
            <th>message</th>

        </tr>
    </thead>
    <tfoot>
        <tr>
            <th></th>
            <th>datetime</th>
            <th>message</th>

        </tr>
    </tfoot>
  </table>
  </body>

i am sure echo json_encode($results) is not empty but why cannot shown in data tables ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,106Questions: 1Answers: 10,574 Site admin

    am sure echo json_encode($results) is not empty but why cannot shown in data tables ?

    Not sure - it looks okay from the above. We would need a link to a test case, per the forum rules, or a debugger trace.

    Allan

  • matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0
    edited June 2016

    the image is debug results ,what's problem ?

  • matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0
    edited June 2016

    edit Sorry. I managed to delete this post by mistake :-(

  • allanallan Posts: 64,106Questions: 1Answers: 10,574 Site admin

    If the debugger detects no tables (you can just give me the 6 letter debug code rather than screenshots btw) it suggests that an error has occurred during the table's initialisation. If that is the case an error should be shown in your browser's console.

    but the code is pass the parameter to response.php ? is it wrong ?

    I think that looks okay, but the browser's console will tell you for sure.

    Allan

  • matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0
    edited June 2016

    hi allan,
    now shown the error "Invalid json response " but the status is 200
    Debugger /Server interaction / status is shown

     < !doctype html > < html > < head > < meta charset = "utf-8" > < title > < /title>
     </head >
    
      < body >
     {
       "hittotal":69511,
         "data":[
            {
       "message":"#, @varbind_list=[#, #, #, #], @specific_trap=6,@source_ip=\"1.2.3.4\", @agent_addr=#, @generic=6>",
       "@timestamp":"2016-06-21T02:29:47.455Z",
        "datetime":"2016-06-21T10:29:47.455Z",
       "type":"trap","datetime":"2016-06-21T10:29:47.455Z",
        SENTRY2_MIB::sentry2ChainLocation":"LA",
        "Sentry2BoardId":"D","sentry2PortPowerAction":"2",
       "SENTRY2_MIB::sentry2PortDeviceName":"x3",
       "SENTRY2_MIB::sentry2PortModuleStatus":"Off Fail",
        "ip":"2.3.4.5",
        "enterprise":"1.3.6.1.4.1.1718.2.100",
        "id":"1.3.6.1.4.1.1718.2.100.0.6"
       },.....
     ]
    

    }

  • allanallan Posts: 64,106Questions: 1Answers: 10,574 Site admin

    If you pass that through JSONLint it will confirm that the above is not valid JSON.

    You'd need to remove the HTML stuff at the start to make it valid.

    Allan

  • matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0

    Error: Parse error on line 1:

  • matt.crawfoordmatt.crawfoord Posts: 31Questions: 13Answers: 0
    edited June 2016

    is copy all to jsonlint ? include the html tag ??

  • allanallan Posts: 64,106Questions: 1Answers: 10,574 Site admin
    Answer ✓

    Apologies if this wasn't clear:

    You'd need to remove the HTML stuff at the start to make it valid.

    This is valid:

    {
      "hittotal":69511,
        "data":[
           {
      "message":"#, @varbind_list=[#, #, #, #], @specific_trap=6,@source_ip=\"1.2.3.4\", @agent_addr=#, @generic=6>",
      "@timestamp":"2016-06-21T02:29:47.455Z",
       "datetime":"2016-06-21T10:29:47.455Z",
      "type":"trap","datetime":"2016-06-21T10:29:47.455Z",
       SENTRY2_MIB::sentry2ChainLocation":"LA",
       "Sentry2BoardId":"D","sentry2PortPowerAction":"2",
      "SENTRY2_MIB::sentry2PortDeviceName":"x3",
      "SENTRY2_MIB::sentry2PortModuleStatus":"Off Fail",
       "ip":"2.3.4.5",
       "enterprise":"1.3.6.1.4.1.1718.2.100",
       "id":"1.3.6.1.4.1.1718.2.100.0.6"
      },.....
    ]
    

    with the doctype and HTML stuff it isn't - as JSONLint says.

    Allan

This discussion has been closed.