2

i want to show json data in the jquery data table , but its not showing any records. throwing error

Cannot read property 'length' of undefined

sample data i have attached my web api code.kindly check and where i need to edit to add "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"}]

code

<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>

web api code

[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);
            }
        }
2
  • Are you sure you are showing us the correct code that throws your error? Because to me your error 'Cannot read property 'length' of undefined' indicates that you are using the .length method on an object or variable that has not been defined yet. But in the code you show I don't see the .length that is indicated by the error Commented Dec 12, 2018 at 10:09
  • yes i'm sure that, the same code im using. its a issue of data attribute is not in the json response.dont know how to add data attribute in the back end. Commented Dec 12, 2018 at 10:41

1 Answer 1

2

Your data must be inside an object with a data attribut :

{
    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" }]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.