Datatable split my result from an API with XMLHTTP request
Datatable split my result from an API with XMLHTTP request
Jeffrey23
Posts: 1Questions: 1Answers: 0
I had been getting a big issue all this past days.
I create an API with a GET method in C# to publish the name of some files from a directory.
But the fact is that I call the API with an XMLHTTP and the result in my datatable is that is just split all the letters in the json response.
This is the js file where I make the call of the API:
var xmlhttp = new XMLHttpRequest();
var url = "urlApi";
var dataset;
xmlhttp.open("GET", url, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = this.responseText;
var receivedData = data.toString();
var dataFinal = JSON.parse(receivedData);
alert(dataFinal);
//var dataT = data.toString().split(',');
/*
for (var i = 0; i < dataT.length; ++i) {
dataset = dataT[i];
*/
/*
var displayresult = [];
alert(dataFinal.length);
for (var i = 0; i <= dataFinal.length; ++i) {
displayresult.push(data[i])
}
*/
//displayresult.push(data)
/*
alert("display" + displayresult);
alert(displayresult);
*/
$(document).ready(function () {
$("#files_id").DataTable({
scrollX: true,
retrieve: true,
"data": dataFinal,
language: {
lengthMenu: "Mostrar _MENU_ registros",
zeroRecords: "No se encontraron resultados",
info: "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
infoEmpty:
"Mostranto registros del 0 al 0 de un total de 0 registros",
infoFiltered: "(filtrado de un total de _MAX_ registros)",
sSearch: "Buscar:",
oPaginate: {
sFirst: "Primero",
sLast: "Último",
sNext: "Siguiente",
sPrevious: "Anterior",
},
sProcessing: "Procesando...",
},
//para usar botones
responsive: "true",
dom: "Bfrtilp",
});
});
}
}
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Answers
Without seeing what the results are of this statement its hard to say what the problem might be. Since you aren't using
columns.data
it suggests that each row is an array. Your data set should look something like this example. Please post an example of whatdataFinal
contains. Please also use Markdown code formatting.Kevin