DataTables warning: table id=Results - Invalid JSON response. For more information about this error,
DataTables warning: table id=Results - Invalid JSON response. For more information about this error,
shobha_rani
Posts: 2Questions: 1Answers: 0
Dear team, below is my code when clicking on Search Button it's showing an invalid JSON Repsone.
Can anyone help me out...
Thanks in advance
function PaginationLoad(data)
{
//$('#Results').DataTable();
let dtData = buildDataTablesData(data);
ActivateSearchResultDataTable(dtData);
}
function buildDataTablesData(data)
{
let parsedJSON = JSON.parse(data);
let finalArray = [];
let finalArrayCols = [];
let respDataObj = parsedJSON.aaData.JSONResponse.Response[0].Data;
let respDataKeys = Object.keys(respDataObj);
let respLength = respDataObj[respDataKeys[0]].length;
for (let i = 0; i < respLength; i++) {
let rowObj = {};
respDataKeys.map(key => {
//console.log(key);
rowObj[key] = respDataObj[key][i];
if(i == 0)
{
finalArrayCols.push({data : key});
}
});
finalArray.push(rowObj);
}
console.log(finalArray);
return [finalArray,finalArrayCols,parsedJSON.iTotalRecords];
}
function ActivateSearchResultDataTable(jsonData)
{
//$('#Results').DataTable();
let ssUrl = Page["Search"].SearchURL + (Page["Search"].SearchURLPaginationParams || "");
$("#Results thead tr").prepend( "<th>Row No.</th>" );
//$("#Results thead th").css("display","inline");
var tableOld = $('#Results').DataTable();
tableOld.destroy();
var table = $('#Results').on('preXhr.dt', function ( e, settings, data ) {
data.columns = "";
data.order = "";
data.search = "";
} ).DataTable({
"processing": true,
"serverSide": true,
ajax: ssUrl,
'dataSrc' : function(data)
{
//return json;
return buildDataTablesData(data)[0];
}
});
$('#Results').on( 'length.dt', function ( e, settings, len ) {
//console.log( 'New page length: '+len );
if(Page["Search"].SearchURL != undefined)
{
Page["Search"].SearchURLPaginationParams = "&draw=" + len + "&start=" + table.page.info().page;
}
});
$('#Results').on( 'page.dt', function () {
//var info = table.page.info();
//console.log( 'Showing page: '+info.page+' of '+info.pages );
if(Page["Search"].SearchURL != undefined)
{
Page["Search"].SearchURLPaginationParams = "&draw=" + table.page.info().length + "&start=" + table.page.info().page;
}
});
}
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Have you followed the steps in the technical notes linked to in the error? That'll be the place to start,
Colin
yes but i am not able to figure it out which dataSrc should be called
'dataSrc' : function(data)
{
//return json;
return buildDataTablesData(data)[0];
}
Please can you the start of the data returned from your server-side script, that will help us understand the format of your data,
Colin