【问题标题】:jquery datatable is not showing any records of json array datajquery datatable 没有显示任何 json 数组数据的记录
【发布时间】:2019-05-13 09:32:59
【问题描述】:

我想在 jquery 数据表中显示 json 数据,但它没有显示任何记录。抛出错误

Cannot read property 'length' of undefined

样本数据 我已附上我的网络 api 代码。请检查我需要编辑以添加“数据”的位置。

[{"id":79,"updatedDate":"2018-12-11T15:34:32","DeviceTime":null,"deviceid":1,"fingerid":1,"message":"ID 1 enrolled","devicename":"FingerScan","status":"IN"},{"id":80,"updatedDate":"2018-12-11T15:34:41.313","DeviceTime":null,"deviceid":1,"fingerid":1,"message":"ID 1 enrolled","devicename":"FingerScan","status":"OUT"},{"id":81,"updatedDate":"2018-12-11T15:34:46.893","DeviceTime":null,"deviceid":1,"fingerid":1,"message":"ID 1 enrolled","devicename":"FingerScan","status":"INVALID"}]

代码

<script>
      $(document).ready(function () {
            $('#myTable').DataTable({
                "ajax": {
                    "url": "/api/Attendance",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                        { "data": "id", "autoWidth": true },
                        {
                            "data": "updatedDate", "autoWidth": true, render: function (data, type, row) {
                                return moment(row.updatedDate).format('DD/MM/YYYY hh:mm:ss');
                            }
                        },
                        { "data": "DeviceTime", "autoWidth": true },
                        { "data": "deviceid", "autoWidth": true },
                        { "data": "fingerid", "autoWidth": true },
                        { "data": "message", "autoWidth": true },
                        { "data": "devicename", "autoWidth": true },
                        { "data": "status", "autoWidth": true },
                ]
            });
        });

</script>

网页接口代码

[Route("api/Attendance")]
        public HttpResponseMessage GetTemperature()
        {
            try
            {
                var gpsJson = "";
                using (kernels1_itiEntities DB = new kernels1_itiEntities())
                {
                    var temp = DB.attendances.ToList();
                    gpsJson = JsonConvert.SerializeObject(temp);
                }
                var response = this.Request.CreateResponse(HttpStatusCode.OK, gpsJson);
                response.Content = new StringContent(gpsJson, Encoding.UTF8, "application/json");
                return response;
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }
        }

【问题讨论】:

  • 您确定向我们展示了引发错误的正确代码吗?因为对我来说,您的错误“无法读取未定义的属性“长度””表明您正在对尚未定义的对象或变量使用 .length 方法。但是在您显示的代码中,我没有看到错误指示的 .length
  • 是的,我确定,我正在使用相同的代码。它的数据属性问题不在json响应中。不知道如何在后端添加数据属性。

标签: javascript jquery asp.net-web-api datatables


【解决方案1】:

您的数据必须在具有数据属性的对象内:

{
    data:
    [{ "id": 79, "updatedDate": "2018-12-11T15:34:32", "DeviceTime": null, "deviceid": 1, "fingerid": 1, "message": "ID 1 enrolled", "devicename": "FingerScan", "status": "IN" }, { "id": 80, "updatedDate": "2018-12-11T15:34:41.313", "DeviceTime": null, "deviceid": 1, "fingerid": 1, "message": "ID 1 enrolled", "devicename": "FingerScan", "status": "OUT" }, { "id": 81, "updatedDate": "2018-12-11T15:34:46.893", "DeviceTime": null, "deviceid": 1, "fingerid": 1, "message": "ID 1 enrolled", "devicename": "FingerScan", "status": "INVALID" }]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2021-06-13
    • 2017-10-20
    • 2020-05-12
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多