My Ajax data won't get load properly into the datatable. And I think it's because the URL I insert into DataTables doesn't give me the proper JSON data. But for my AJAX formula it is the correct URL...
The AJAX Formula:
$.get(
ajax_url,
data,
function(data) { // AJAX callback
console.log(data);
fill_json(data);
});
You I get JSON data that originally fill in using the function down below:
var fill_json = function(data){
var jsonData=$.parseJSON(data); //change here
$.each(jsonData, function(i) {
var tblRow = "<tr>" + "<td>" + jsonData[i].ID + "</td>" +
"<td>" + jsonData[i].post_title + "</td>" + "<td>";
$(tblRow).appendTo("#testajax tbody");
});
};
The data is rootless JSON:
[{"ID":"4066","post_title":"TATTOO SLEEVES","category":"Apparel & Accessories","slug":"apparel-and-accessories","supplier_company":"\u682a\u5f0f\u4f1a\u793e\u30bb\u30f3\u30bf\u30fc\u5546\u4e8b","allslug":"apparel-and-accessories, accessories, textiles, toys, gifts-toys-and-animation, english","allcatname":"Apparel & Accessories, Accessories, Textiles, Toys, Gifts, Toys & Animation, \u82f1\u8a9e"}]
Then I have the Datatable function which I coded just like in the guide (columns set from root):
$( document ).ready(function() {
var t2e = $('#testajax').DataTable({
"ajax": {
"url":"<?php echo admin_url( 'admin-ajax.php' ); ?>",
"dataSrc":""
},
"columns": [
{ "data" : "ID" },
{ "data" : "name" },
],
});
});
With my function fill_json(data);
my data appears, but this is not initiated properly into the DataTable. If I comment this out nothing happens on the Ajax call. Datatables doesn't fill in the table...
DataTables isn't able to fill the data from this link: "url":"<?php echo admin_url( 'admin-ajax.php' ); ?>",
But it is the same link as my AJAX call... Any ideas?