Kali Kali - 2 months ago 7
Ajax Question

JSON Object to Datatables.js

I have a problem about populating datatables.js using json object, Can I use the ajax response value as the data for the datatable?

$.ajax({
url:"php_action/searchloc.php",
type:"POST",
data:{
origin: $('#from').val(),
destination: $('#to').val()
} ,
dataType:"json",
success:function(data){

$('#schedule').dataTable( {
"ajax": data,
columns: []
} );
},
error:function(){
alert("error");

}
});

Answer

DataTables has it's own AJAX functionality that uses jQuery's AJAX method, so you don't need to wrap your dataTable logic inside of a jQuery AJAX call.

$('#schedule').dataTable( {
  "ajax": {
    url:"php_action/searchloc.php",
    type:"POST",
    data:{
      origin: $('#from').val(),
      destination: $('#to').val()
    },
    dataType:"json",
    columns: []
  }
} );
Comments