gpt4 book ai didi

javascript - jquery 数据表不显示 json 数组数据的任何记录

转载 作者:行者123 更新时间:2023-11-29 20:47:38 25 4
gpt4 key购买 nike

我想在 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>

网络接口(interface)代码

[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);
}
}

最佳答案

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

{
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" }]
}

关于javascript - jquery 数据表不显示 json 数组数据的任何记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53740308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com