【发布时间】:2019-05-27 08:15:46
【问题描述】:
我看过这个问题:Jquery Datatables Error: Invalid JSON primitive: draw
它无法帮助我解决问题。
我正在使用 Datatables,我收到 AJAX 调用错误。这是JS代码:
function loadparttrackings() {
// Call the datatable on the overview div and link back to server side
$('#overviewFilteredPartTrackings').DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"orderMulti": false,
"ajax": {
"url": window.applicationPath + "/Launches/GetFilteredPartTrackingRecords",
"type": "POST",
"data": function (d) {
d.partNumber = $('#FilterPartNumber').val();
d.segment = $('#FilterSegment').val();
d.commodity = $('#FilterCommodity').val();
}
},
// This is needed to hide the ID field and make it not searchable
"columnDefs":
[{
"targets": [1],
"visible": false,
"searchable": false
}],
"columns": [
{
"render": function (data, type, full, meta) { return '<a class="btn btn-info" href="#" onclick=EditRecord("' + row.ID + '"); >Edit</a>'; }
},
// more columns omitted
});
}
当我将"type": "POST", 添加到函数的 AJAX 部分时,问题出现了。现在我收到错误invalid json primitive draw。
这里是控制器方法的代码:
[HttpPost]
public ActionResult GetFilteredPartTrackingRecords(DataTableAjaxPostModel model, string partNumber, string commodity, string segment)
{
// Getting the data and such
return Json(new
{
// this is what datatables wants sending back
draw = model.draw,
recordsTotal = 1000, // TODO CHANGE
recordsFiltered = 1000, // TODO CHANGE
data = result
});
}
这是我使用的模型的代码:
public class DataTableAjaxPostModel
{
// properties are not capital due to json mapping
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public Search search { get; set; }
public List<Order> order { get; set; }
}
public class Column
{
public string data { get; set; }
public string name { get; set; }
public bool searchable { get; set; }
public bool orderable { get; set; }
public Search search { get; set; }
}
public class Search
{
public string value { get; set; }
public string regex { get; set; }
}
public class Order
{
public int column { get; set; }
public string dir { get; set; }
}
【问题讨论】:
标签: c# json ajax datatables