Control error: Invalid JSON response. In dataTables

Control error: Invalid JSON response. In dataTables

NanoDevNanoDev Posts: 3Questions: 1Answers: 0

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

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    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.

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin

    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

  • NanoDevNanoDev Posts: 3Questions: 1Answers: 0

    Oh! Thanks now validate my server-side script for me to return json in case there are no records. thank you very much

  • shariq619shariq619 Posts: 1Questions: 0Answers: 0

    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();
    }
    }
    });

This discussion has been closed.