数据表 - 在数据表中加载JSON响应

时间:2013-04-17 12:13:58

标签: java javascript jquery json datatables

使用REST API,我从下面的Ajax命中获取JSON响应以加载数据表。

我收到错误:Uncaught TypeError: Cannot read property 'length' of undefined.

$('#example').dataTable({
    "bSort" : false,
    "bDestroy" : true,
    "sPaginationType" : "full_numbers",
    "bProcessing" : false,
    "bFilter" : true,
    "bServerSide" : true,
    "bAutoWidth" : false,
    "sAjaxSource" : 'exampledatagrid.json?token=' + sessionId,
    "aoColumns" : [{
        "mData" : "0",
        "mData" : "1",
        "mData" : "2",
        "mData" : "3"
    }],
    "fnServerParams" : function(aoData) {
        alert(aoData);
        aoData.push({
            "name" : "colNameMode",
            "value" : "userStatusList"
        }, {
            "name" : "colName_0",
            "value" : "userIDList"
        }, {
            "name" : "colName_1",
            "value" : "userNameList"
        }, {
            "name" : "colName_2",
            "value" : "userRemarksList"
        });
    },
    "fnRowCallback" : function(nRow, aData, iDisplayIndex) {
        alert(aData);
    },
    "fnDrawCallback" : function(oSettings) {
        if ( oSettings.bSorted || oSettings.bFiltered ) {
            for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
                $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
            }
        }
    }
});

JSON响应是:

{"userRemarksList":["TECHNOLOGY","GUEST","HOUSE","SECURITY"],"userIDList":[23,45,23,54],"userNameList":["Michael","Danny","Rahil","Ramesh"],"userStatusList":[0,0,1,1]} 

我想知道是否必须更改我的JSON格式,或者是否可以从上面的JSON响应中检索?

1 个答案:

答案 0 :(得分:1)

我通过使用hibernate“org.hibernate.query”查询结果来改变我的后端。从这个我能够获得记录列表(一个一个)并分别加载到网格中。