【问题标题】:Datatables issue with invalid JSON primitive draw无效 JSON 原始绘制的数据表问题
【发布时间】: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


    【解决方案1】:

    试试这个

    "data":JSON.stringify({
    
                partNumber: $('#FilterPartNumber').val();
                segment: $('#FilterSegment').val();
                commodity: $('#FilterCommodity').val();
       })
    

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

    【讨论】:

    • 您好,这并没有为我解决。不过,我以其他方式修复了它,我会发布答案。
    【解决方案2】:

    我通过以下操作修复了它:

    从 ajax 调用中删除 type: post。

    "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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2016-04-29
      • 1970-01-01
      • 2017-04-26
      • 2020-05-29
      相关资源
      最近更新 更多