javascript - jquery datatable gives error when rendering -


i'm trying fill jquery datatable data via ajax:

html

<table id="table-productmaterials">     <thead>         <tr>             <th>id</th>             <th>name</th>             <th>quantity</th>             <th>status</th>         </tr>     </thead> </table> 

javascript

$(document).ready(function () {     var options = {         "processing": true,         "ajax": {             "url": "productmaterials.ashx?action=get",             "type": "post",             "data": {                 "productid": $('#product_id').val()             },             "columns": [                         { "data": "id" },                         { "data": "materialname" },                         { "data": "quantity" },                         { "data": "status" }             ]         },     };     table = $('#table-productmaterials').datatable(options); }); 

generic handler output:

{"data": [{"id":1,"quantity":15.00,"status":"1","materialname":"iron","productname":"french onion soup"},{"id":3,"quantity":14.00,"status":"1","materialname":"nails","productname":"french onion soup"}]} 

error message when reloading data: datatables warning: table id=table-productmaterials - requested unknown parameter '0' row 0. more information error, please see http://datatables.net/tn/4

i refered page , read example can't seem resolved. i'm making wrong?

you have place columns property outside of ajax property this:

$(document).ready(function () {     var options = {         "processing": true,         "ajax": {             "url": "productmaterials.ashx?action=get",             "type": "post",             "data": {                 "productid": $('#product_id').val()             },         },         "columns": [                                 { "data": "id" },                                 { "data": "materialname" },                                 { "data": "quantity" },                                 { "data": "status" }         ]     };     table = $('#table-productmaterials').datatable(options); }); 

then work.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

Simple Angular 2 project fails 'Unexpected reserved word' -

php - Get process resource by PID -