Struggling to bind JSON data
Struggling to bind JSON data
themag87
Posts: 2Questions: 1Answers: 0
I am struggling to bind the JSON data into a Datatable. This is my Javascript code.
$.ajax({
type: "POST",
data: $('#searchForm').serialize(),
url: "/ReportingItem/Search",
success: function (data) {
$('#searchResults').DataTable({
data: data,
dataSrc: "",
columns: [
{ "data": "ID" },
{ "data": "ItemContent" }
]
});
},
error: function (xhr, ajaxOptions, thrownError) {
if (xhr.status == 404) {
alert(thrownError);
}
}
});
This is my JSON
"[{"ID":"3","ItemContent":"2nd Test"},{"ID":"4","ItemContent":"3rd Test"},{"ID":"9","ItemContent":"eeeeee"},{"ID":"11","ItemContent":"aaaa"}]"
You can see its a flat array. therefore I have used dataSRC="" so it doesn't look for "data" in the JSON
This is the error message I am getting
! DataTables warning: table id=searchResults - Requested unknown parameter 'ID' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
Any help on the Syntax would be helpful!
Thanks
Chris
This discussion has been closed.
Answers
Just add in the data line
$('#searchResults').DataTable({
data: JSON.parse(data),
....
});