使用datatable和node.js

我正在尝试使用jQuery数据表与Node.js. 这是我的代码HTML

<button id="btnSearch" type="submit" class="btn btn-responsive"><i class="icon-search"></i>&nbsp;Search</button> <div class="box-body table-responsive no-padding"> <div id="tableContainer"> <table class="table table-hover" id="dataTables1"> <thead> <tr> <th class="text-left">base</th> <th class="text-left">base1</th> </tr> </thead> <tbody></tbody> </table> </div> <div class="text-center"> <img id="loading-image" src="../dist/img/loading_spinner.gif" style="display: none;" /> </div> </div> 

这里是脚本代码

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $("#btnSearch").click(function () { RefreshTable("#dataTables1"); }); function RefreshTable(tableId) { table = $(tableId).dataTable(); table._fnClearTable(table.oSettings); table.fnDestroy(); table.fnDraw(true); var table2 = $(tableId).dataTable({ "bServerSide": true, "bProcessing": true, "responsive": true, "bAutoWidth": false, oLanguage: { sProcessing: "<img src='../dist/img/loading_spinner.gif'/>" }, "aLengthMenu": [10, 30, 50, 100], "pageLength": 30, "sAjaxSource": 'http://127.0.0.1:3000/statistic?schemename=abc', "fnServerData": function (sSource, aoData, fnCallback) { $.ajax({ "dataType": 'json', "type": "POST", "url": sSource, "data": aoData, "success": fnCallback }); }, "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'dataTables_wrapper'ip>>", "sPaginationType": "full_numbers",//"full_numbers", "aoColumns": [ { "sName": "base", "bSortable": false },//0 { "sName": "base1", "bSortable": false } ] }); } </script> 

这里是我的代码服务器端节点js

 var async = require('async'), QueryBuilder = require('datatable'); var tableDefinition = { sTableName: 'Orgs' }; var queryBuilder = new QueryBuilder(tableDefinition); // requestQuery is normally provided by the DataTables AJAX call var requestQuery = { iDisplayStart: 0, iDisplayLength: 5 }; // Build an object of SQL statements var queries = queryBuilder.buildQuery(requestQuery); exports.test = function(req, res){ console.log(req.query.schemename); userExport.getUserEdit(req.query.schemename, function(rows){ res.json(queryBuilder.parseResponse(rows)); }) } 

当我点击searchbutton,然后在服务器端我收到schemename是'abc',并得到了用户,但我不能响应json表。 在浏览器的标签控制台中,我得到了一个错误:jquery.dataTables.js:4108 Uncaught TypeError:无法读取未定义的属性“长度”,任何人都帮我出这个错误或build议我一个解决scheme来解决这个问题感谢冒险

sAjaxDataProp: 'data'添加到您的初始化参数中:

 var table2 = $(tableId).dataTable({ ... sAjaxDataProp: 'data', sAjaxSource: 'http://127.0.0.1:3000/statistic?schemename=abc', ... }) 

原因:在使用sAjaxSource ,dataTables 1.9.x期望数据格式为{ "aaData" : [...] } ,这就是为什么你得到Uncaught TypeError:无法读取未定义的属性'length' 。 但是, node-datatable将行导出为{ "data" : [...] }