无效的JSON基本绘图导致数据表问题

时间:2019-05-27 08:15:46

标签: c# json ajax datatables

我已经看到了这个问题:Jquery Datatables Error: Invalid JSON primitive: draw

它不能帮助我解决问题。

我正在使用数据表,但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
    });
}

当我向函数的AJAX部分添加"type": "POST",时出现问题。现在,我收到错误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; }
}

2 个答案:

答案 0 :(得分:0)

尝试一下

"data":JSON.stringify({

            partNumber: $('#FilterPartNumber').val();
            segment: $('#FilterSegment').val();
            commodity: $('#FilterCommodity').val();
   })

并在ajax选项中添加contentType: "application/json; charset=UTF-8"

答案 1 :(得分:0)

我通过以下操作对其进行了修复:

删除类型:从ajax调用中发布。

"ajax": {
            "url": window.applicationPath + "/Launches/GetFilteredPartTrackingRecords",
            "data": function (d) {
                d.partNumber = $('#FilterPartNumber').val();
                d.segment = $('#FilterSegment').val();
                d.commodity = $('#FilterCommodity').val();
            }
        },

更改控制器方法以获取

[HttpGet]
    public ActionResult GetFilteredPartTrackingRecords(DataTableAjaxPostModel model, string partNumber, string commodity, string segment)

更改JSON请求的行为,以便我可以通过GET返回

return Json(new
        {
            // this is what datatables wants sending back
            draw = model.draw,
            recordsTotal = 1000, // TODO CHANGE
            recordsFiltered = 1000, // TODO CHANGE
            data = result
        }, JsonRequestBehavior.AllowGet);

我不知道这是否是最好的/好的解决方案,但这对我有用。