json data from server cannot be parsed
json data from server cannot be parsed

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?
This discussion has been closed.
Answers
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
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 ?
} );
I don't know what the issue was. Could you explain what the problem with
json_encode
was please.