Control error: Invalid JSON response. In dataTables
Control error: Invalid JSON response. In dataTables
I'm loading my data that I collect in JSON format to my datatables, and I'm checking if it comes empty, it works fine when the data exists, but when it does not keep the message '' searching .. ", I need to modify something in my fnCallback.
$('#data').dataTable({
"sAjaxSource": 'user.php',
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ){
oSettings.jqXHR = $.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": function(result){
if (result.aaData === null){
result.aaData = [];
}
console.log(result);
fnCallback(result);// draw the table
}
});
}
});
Replies
unless you are using an older version of datatables, you probably should take a look at some of the ajax examples and adjust accordingly. It looks like you are using more code than you need to.
Your sever side code should return an empty array if there is now rows found, though you can intercept and watch for null if needed.
Agreed - it sounds like the server isn't always sending back valid JSON. Either your custom Ajax handler needs to handle that, or (and this is the option I would suggest) you update the server-side script to always return valid JSON, even in the case where there are no records.
Allan
Oh! Thanks now validate my server-side script for me to return json in case there are no records. thank you very much
I Think you should use error parameter just like in below code..
mytableHSX = $("#mytableHSX").DataTable({
//"aaSorting": [[ 2, "asc" ]],
"language" : {
sLoadingRecords : '<span id="lodr" style="width:100%;"><img src="img/loader.gif"></span><span id="msg"></span>',
emptyTable: 'No Records Found'
},
"ordering": false,
"ajax": {
'type': 'POST',
'url': 'includes/retrieve-Hscode.php',
'data': {
hscode: '<?php echo $_POST["hscode"] ?>',
HsLikeEx: '<?php echo $_POST["HsLikeEx"] ?>'
},
'error': function(){
$('#lodr').hide();
$('#msg').append('No Records Found').show();
}
}
});