json data from server cannot be parsed

json data from server cannot be parsed

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

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

Here is the response.php

 $qryurl = '<myurl>/web/_search?scroll=10s&size=10';
$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $qryurl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, true); 
        curl_setopt($ch,CURLOPT_HEADER,false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $return = curl_exec($ch) or die(curl_error());
        $array_return = json_decode($return,true);
        curl_close($ch);    
        $hittotal = $array_return['hits']['total'];
        $source = $array_return['hits']['hits'];
        $sourcecount=count($array_return['hits']['hits']);
        $sourceary = array();
        for($i=0;$i<$sourcecount;$i++)
        {
            $msgary = $array_return['hits']['hits'][$i]['_source'];     
            array_push($sourceary,$msgary);
        }
         $results = array(
                    "sEcho" => 1,
                  "iTotalRecords" => $hittotal,
                "iTotalDisplayRecords" => $hittotal,
                    "aaData"=>$sourceary
                    );


        echo json_encode($results);

javascript :

           $('#example').DataTable( {
                                "bProcessing": true,
                                 "sAjaxSource":"response.php",
                               "aoColumns": [
                                { mData: 'datetime' } ,
                               { mData: 'message' }
                                      ]

                        } );

html :

      <head>
      <script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-1.12.3.js">    </script></head>
     <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>

but from the response.php echo ,the data is valid json
How can I get DataTables to work with my current data? What are any potential errors that I am overlooking?

Answers

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

    Can you use the debugger or give a link to the page showing the issue please.

    One possible explanation is that the JSON includes the UTF-8 BOM, which stops jQuery's parser from working. But without a test case (per the forum rules :-) ) it is impossible to say.

    Allan

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

    hi allan,
    i found the problem is about json_encode echo format.
    but now i have a problem is how to load large data into jquery datatables?
    i find the scroll looks like can solve the problem , but how to add it to my code ?

     $(document).ready(function() {
    
     var table = $('#example').DataTable( {
        "ajax": "response.php",
        "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');
        }
    } );
    

    } );

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

    but now i have a problem is how to load large data into jquery datatables?

    I don't know what the issue was. Could you explain what the problem with json_encode was please.

This discussion has been closed.