I have a jquery-datatables that I am populating from a method in C#.
The C# method returns some json which I have validated. However my columns never get populated and instead I get a message that says No data available in table.
I'm completely stuck so hoping someone can help!
var table = $('#example').DataTable({
ajax: {
url: pageUrl,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
dataSrc: function (data) {
console.log(data.d);
return $.parseJSON(data.d);
}
},
"pageLength": 50,
fixedHeader: true,
responsive: true,
"columns": [
{
"data": "key",
"render": function (data, type, row, meta) {
if (type === 'display') {
data = '<a href="/software/details.aspx?id=' + data + '">' + data + '</a>';
}
return data;
} },
{ "data": "summary" },
{ "data": "created" },
{ "data": "updated" },
{ "data": "Status" },
{ "data": "priority" },
{ "data": "reporter" },
{ "data": "assignee" }
],
autoWidth: false,
"columnDefs": [
{ "width": "50%", "targets": 0 },
{ "width": "5%", "targets": 1 },
{ "width": "5%", "targets": 2 },
{ "width": "5%", "targets": 3 },
{ "width": "5%", "targets": 4 },
{ "width": "5%", "targets": 5 }
],
"order": [[1, 'asc']],
"success": fnsuccesscallback,
"error": fnerrorcallback
});
function fnsuccesscallback(data) {
alert(data.d);
}
function fnerrorcallback(result) {
alert(result.statusText);
}
And this is the data in the console.log output:
{"summary":"External Change Request Form: tester - 19/1/2021","created":"2021-01-19T15:32:02+00:00","updated":"2021-06-03T08:59:17+01:00","status":"To Do","key":"ITS-4711","assignee":"Bob","priority":"Low","reporter":"Dave"}
data.dis already a javascript object, not a JSON string, especially withdataType:'json'- try changingreturn $.parseJSON(data.d);to justreturn data.d