Load data for the table's content from an Ajax call
Load data for the table's content from an Ajax call
Hello Everyone. This is my first time using this forum. I am Abdullah. You can call me Abdul or Abdullah. I have a problem regarding the data table and ajax call. Basically, I want to load the content for the datatable using the existing ajax call to load the content rather than making another ajax call(repetition). I am going to show my code below. My code include the ajax code and the jquery datatable code. Both of the ajax and the datatable are in the same file. I want both of the code to be together...
AJAX Call
function showJSslice() {
$.ajax({
type: "POST",
dataType: "JSON",
url: "/project-data/pass.php",
async: false,
data: JSON.stringify({
id:$("id").val(),
}),
success: function(data) {
jsonData=data.slice();
},
error: function(jqXHR, textStatus, errorThrown) {
alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
$('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
},
});
}
JQUERY Datatable
var alltable = $('#attacks').DataTable( {
"pageLength" : 4,
"lengthChange": false,
"info" : false,
"retrieve" : true,
"responsive" : true,
"searching" : false,
"ajax": {
"url": "/project-data/pass.php",
"dataSrc": function (json) {
var return_data = new Array();
for(var i=0;i< json.length; i++){
var timestamp = json[i]["time"];
//console.log("TTT " +timestamp);
//var date = new Date(timestamp*1000 +(480*60000));
var date = new Date(timestamp*1000);
var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/);
return_data.push({
'time': iso[1] + ' ' + iso[2] + ' '+ timestamp,
'sourceCountry' : json[i]["country.src"],
'destCountry' : json[i]["country.dst"]
})
}
return return_data;
}
},
"columns": [
{ "data": 'time' },
{ "data": 'sourceCountry' },
{ "data": 'destCountry' }
],
"order" : [[0, "desc" ]]
});
How am I supposed to use that ajax call in datatable code???
Best Regards,
abdullah.s.n
Replies
My main question is how to link the ajax call to the datatable. Sorry... My English is bad.....