javascript DataTables 警告(表 id = 'example'):从第 0 行的数据源请求未知参数“0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21552041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:08:41  来源:igfitidea点击:

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

javascriptdatatable

提问by ankit_tyagi

i am very new to javascript and jquery and using data table to show server data. i am using below code.

我对 javascript 和 jquery 非常陌生,并使用数据表来显示服务器数据。我正在使用下面的代码。

    $(document).ready(function () {
     $("#example").dataTable({
         "bProcessing": true,
         "sAjaxSource": "/admin/vskuStatusUid?uploadId=" + $('#UID').val(),
         "aoColumns": [{
             "mData": "uid"
         }, {
             "mData": "vcode"
         }, {
             "mData": "vsku"
         }, {
             "mData": "timeStamp"
         }, {
             "mData": "state"
         }, {
             "mData": "counter"
         }]
     });
 });

and my ajax response looks like below 

{
    "aaData": [
        {
            "uid": "UID0000007017",
            "vcode": "927ead",
            "vsku": "Prateek1000",
            "timeStamp": 1391158258658,
            "state": "VENDOR_PRODUCT_PERSISTENCE_COMPLETED",
            "counter": 2
        },
        {
            "uid": "UID0000007017",
            "vcode": "927ead",
            "vsku": "Prateek5000",
            "timeStamp": 1391158258881,
            "state": "VENDOR_PRODUCT_PERSISTENCE_COMPLETED",
            "counter": 3
        }
    ]
}

and my hmtl code is below

我的 hmtl 代码在下面

<table id="example">
        <thead>
            <tr>
            <th>Upload Id</th>
            <th >Vcode</th>
            <th>Vsku</th>
            <th>Timestamp</th>
            <th>State</th>
            <th>counter</th>
            </tr>
          </thead>
          <tbody>
        </tbody>
        </table>

can someone help me out here.

有人可以帮助我吗?

i have checked other answer related to this question and mostly all were indicating the problem might be difference in thead total column and mdata.

我已经检查了与这个问题相关的其他答案,大多数都表明问题可能是总列和 mdata 的差异。

回答by Ori Refael

you dont need to write the columns in the html, dataTable does it for you. the only html you need is <table id="example"></table>

您不需要在 html 中编写列,dataTable 会为您完成。你唯一需要的 html 是<table id="example"></table>

i think there error is the you insert data partially or trying to get data from unexisted row in the table.

我认为错误是您部分插入数据或尝试从表中不存在的行获取数据。

here is a possible fix :

这是一个可能的解决方法:

after you grab the data you got and parse it to object. you can do something like this:

在获取您获得的数据并将其解析为对象之后。你可以做这样的事情:

var table = $("#example").dataTable({
         "bProcessing": true,
         "sAjaxSource": "/admin/vskuStatusUid?uploadId=" + $('#UID').val(),
         "aoColumns": [{
             "mData": "uid"
         }, {
             "mData": "vcode"
         }, {
             "mData": "vsku"
         }, {
             "mData": "timeStamp"
         }, {
             "mData": "state"
         }, {
             "mData": "counter"
         }]
     });

for (var i=0; i< ParsedObject.length; i++) {
    var temp_item = ParsedObject[i]; //new row data
    table.fnAddData(temp_item.uid, temp_item.vcode, temp_item.vsku, temp_item.timeStamp, temp_item.state, temp_item.counter); //adds new row to datatable
}