字段名称使用JSON,但数据表无法识别?

时间:2018-11-14 06:35:22

标签: javascript jquery json datatables

错误信息如下:

  

第0行第0列请求的未知参数'emPONumber'

但是emPONumber在JSON中,为什么数据表仍然提示此错误?

<script type="text/javascript">
    $(document).ready(function () {
        var tableId = 'tablePurchaseOrders';
        var table = $('#' + tableId).DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                url: 'http://localhost/ControlTower2WebAPI/api/PurchaseOrder/GetAllUploadedPurchaseOrders',
                type: 'GET',
                data: function (data) {
                    //debugger;
                    var model = {
                        draw: data.draw,
                        start: data.start,
                        length: data.length,
                        columns: data.columns,
                        search: data.search,
                        order: data.order
                    };
                    return model;
                },
                failure: function (result) {
                    debugger;
                    alert("Error occurred while trying to get data from server: " + result.sEcho);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                    alert("Error occurred while trying to get data from server!");
                },
                dataSrc: function (json) {
                    var _data = JSON.stringify(json.Data);
                    debugger;
                    return _data;
                }
            }
            ,
            "columns": [
                { "data": "emPONumber", title: "emPONumber" },
                { "data": "ASMPONumber", title: "ASMPONumber" },
                { "data": "material", title: "material" }
            ]
        });
    });
</script>

json in dataSrc: function (json)

{"ContentEncoding":null,"ContentType":null,"Data":{"draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[{"emPONumber":"EM1234567","ASMPONumber":"A741000013","material":"26-00010","quantity":5,"UOM":"EA","asmPOQuantity":5,"createBy":"m","ASMPOYear":2018,"ASMPOMonth":6,"ASMPOVendor":"10008"}]},"JsonRequestBehavior":1,"MaxJsonLength":null,"RecursionLimit":null}

json(_data)由dataSrc中的ajax返回:

{
    "draw":1,   
    "recordsTotal":1,
    "recordsFiltered":1,
    "data":
        [{
            "emPONumber":"EM1234567",
            "ASMPONumber":"A741000013",
            "material":"26-00010",
            "quantity":5,
            "UOM":"EA",
            "asmPOQuantity":5,
            "createBy":"m",
            "ASMPOYear":2018,
            "ASMPOMonth":6,
            "ASMPOVendor":"10008"
        }]
}

1 个答案:

答案 0 :(得分:1)

尝试

dataSrc: function (json) {
   for(key in json.Data){ json[key] = json.Data[key]; }
   delete json['Data'];

   return json.data;
}