Using ajax to load a jQuery DataTable

前端 未结 2 792
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 19:59

I\'m trying (and failing) to load a jQuery DataTable using the built-in ajax source argument. The datatable, however, shows the message \"Loading...\" where the row(s) shou

2条回答
  •  暖寄归人
    2020-12-16 20:26

    according to the website your service should return data in this format:

    {
      "aaData": [
        [
          "row 1 col 1 data",
          "row 1 col 2 data",
          "row 1 col 3 data",
          "row 1 col 4 data"
        ],
        [
          "row 2 col 1 data",
          "row 2 col 2 data",
          "row 2 col 3 data",
          "row 2 col 4 data"
        ],
        [
          "row 3 col 1 data",
          "row 3 col 2 data",
          "row 3 col 3 data",
          "row 3 col 4 data"
        ],
        [
          "row 4 col 1 data",
          "row 4 col 2 data",
          "row 4 col 3 data",
          "row 4 col 4 data"
        ]
      ]
    }
    

    so, wrap your array in an object, name the array as aaData and try again. or you can name it any way you like, but then you need to add the sAjaxDataProp parameter in the datatables initialisation (say you name it data you would do it like this:

    $('#example').dataTable( {
        "bProcessing": true,
        "sAjaxSource": "/ajaxsource/callmydata",
        "sAjaxDataProp": "data"
    } );
    

提交回复
热议问题