DataTables: Cannot read property 'length' of undefined

前端 未结 11 1211
醉酒成梦
醉酒成梦 2020-12-13 05:15

I understand this a popular issue, and I have read all the similar questions here on Stack Overflow and other sites (including the datatables website).

To clarify, I

11条回答
  •  再見小時候
    2020-12-13 05:55

    CAUSE

    This errors TypeError: Cannot read property 'length' of undefined usually means that jQuery DataTables cannot find the data in the response to the Ajax request.

    By default jQuery DataTables expects the data to be in one of the formats shown below. Error occurs because data is returned in the format other than default.

    Array of arrays

    { 
       "data": [
          [
             "Tiger Nixon",
             "System Architect",
             "$320,800",
             "2011/04/25",
             "Edinburgh",
             "5421"
          ]
       ]
    }
    

    Array of objects

    { 
       "data": [
          {
             "name": "Tiger Nixon",
             "position": "System Architect",
             "salary": "$320,800",
             "start_date": "2011/04/25",
             "office": "Edinburgh",
             "extn": "5421"
          }
       ]
    }
    

    SOLUTION

    Use default format or use ajax.dataSrc option to define data property containing table data in Ajax response (data by default).

    See Data array location for more information.

    LINKS

    See jQuery DataTables: Common JavaScript console errors for more details.

提交回复
热议问题