我想在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>
网络api代码
[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);
}
}
答案 0 :(得分:2)
您的数据必须位于具有数据属性的对象中:
{
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" }]
}