请求的未知参数' 0' 0来自第0行问题的数据源

时间:2014-04-30 10:09:39

标签: javascript jquery json

每次跟踪代码我都会收到警告。

的javascript

$(document).ready(function () {               
        var $lmTable = $("#information").dataTable({
            "sPaginationType": "full_numbers",
            "sScrollX": "1000px",
            "sScrollY": calcDataTableHeight(),
            "sSearch": "Search all:",
            "aaData": [],
            "aoColumns": [
                { "mDataProp": "senderCompID", sDefaultContent: "n/a"},
                { "mDataProp": "targetCompID", sDefaultContent: "n/a"},
                { "mDataProp": "tradSesReqID", sDefaultContent: "n/a"},
                { "mDataProp": "sendingTime", sDefaultContent: "n/a"},
                { "mDataProp": "tradingSessionID", sDefaultContent: "n/a"},
                { "mDataProp": "messageType", sDefaultContent: "n/a"}
            ]
        });

HTML

   <table id="information" class="display">
            <thead>
            <tr>
                <th>SENDER COMP ID</th>
                <th>TARGET COMP ID</th>
                <th>TRADE SESSION REQ ID</th>
                <th>SENDING TIME</th>
                <th>TRADING SESSION ID</th>
                <th>MESSAGE TYPE</th>
            </tr>
            </thead>
            <tbody id="infoBody">
            </tbody>
   </table>

JSON响应

[
 {
    "senderCompID":"1159",
    "targetCompID":"CASE",
    "tradSesReqID":"1308042969531",
    "sendingTime":"Jun 14, 2011 9:00:56 AM",
    "tradingSessionID":"SME",
    "messageType":"g"
  }
]

我在加载页面时遇到以下错误,并且没有加载表格行。

DataTables warning (table id = 'information'): Requested unknown
parameter '0' from the data source for row 0

这里有什么问题,解决方案是什么?

1 个答案:

答案 0 :(得分:0)

<强>诊断

查看Datatables的初始化..您在初始化时将aaData(即数据源)设置为空数组。换句话说,只要数据表生成您的表,它就会尝试从空数组中读取第一列并返回该警告

<强>解决方案

在初始化表之前获取JSON响应。

...    

//Get your json from the server

var aaData = [
{
    "senderCompID":"1159",
    "targetCompID":"CASE",
    "tradSesReqID":"1308042969531",
    "sendingTime":"Jun 14, 2011 9:00:56 AM",
    "tradingSessionID":"SME",
    "messageType":"g"
  }
]

//Then initialise the table

或者......假设您正在使用Ajax

var aaData = null;

$.ajax("*put your url here*")
.done(function(response){
    aaData = response;

    // Then initialise table 
});

以下是使用Ajax获取数据的整个代码;

$(document).ready(function () {    

    $.ajax("*put your url here*")
    .done(function(response){

        //Set the variable aaData to the response
        var aaData = response;

        // Then initialise table using aaData as the datasource

        var $lmTable = $("#information").dataTable({
            "sPaginationType": "full_numbers",
            "sScrollX": "1000px",
            "sScrollY": calcDataTableHeight(),
            "sSearch": "Search all:",
            "aaData": aaData,    // <-- Here
            "aoColumns": [
                { "mDataProp": "senderCompID", sDefaultContent: "n/a"},
                { "mDataProp": "targetCompID", sDefaultContent: "n/a"},
                { "mDataProp": "tradSesReqID", sDefaultContent: "n/a"},
                { "mDataProp": "sendingTime", sDefaultContent: "n/a"},
                { "mDataProp": "tradingSessionID", sDefaultContent: "n/a"},
                { "mDataProp": "messageType", sDefaultContent: "n/a"}
            ]
        });
    });