为什么jQuery dataTables不能解析JSON?

时间:2022-05-09 14:23:55

I am trying to populate a dataTable as follows:

我正在尝试填充一个dataTable,如下所示:

$("#my-datatable").dataTable( {
    "sAjaxSource" : "/someURLOnMyServer",
    "bDestroy" : true,
    "fnServerParams" : function(serverParams) {
        serverParams.push(
            {
                "name" : "widget",
                "value" : token
            }
        );
    }
});

And the HTML table it is populating:

它所填充的HTML表格:

<table id="my-datatable">
    <thead>
        <tr>
            <th>Type</th>
            <th>Value</th>
            <th>ID</th>
            <th>Fizz</th>
            <th>Buzz</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

According to Firebug, the JSON coming back from the server is:

根据Firebug,从服务器返回的JSON为:

[
   {
      "id":1,
      "attributeType":{
         "id":1,
         "name":"test1",
         "tag":"test-type",
         "is-dog":false
      },
      "attributeValue":{
         "id":null,
         "name":"blah",
         "tag":"BLAH"
      },
      "buzz":1,
      "fizz":"53abc"
   }
]

But Firebug is throwing the following JavaScript error in its console:

但Firebug在其控制台中抛出如下JavaScript错误:

TypeError: aData is undefined
[Break On This Error]   

for ( i=0 ; i<aData.length ; i++ ) --> jquery.dataTables.js (line 2541)

Can anyone spot what's going wrong? Either I'm not setting up my dataTable object correctly, or the JSON coming back doesn't match the "schema" of the HTML table it is trying to populate. Either way, I'm lost. Thanks in advance!

谁能指出哪里出了问题吗?要么我没有正确设置dataTable对象,要么返回的JSON与它试图填充的HTML表的“schema”不匹配。无论哪种方式,我迷路了。提前谢谢!

2 个解决方案

#1


9  

Datatables requires a certain format for results. If you are not using that format, you have to declare everything.

datatable需要一个特定的结果格式。如果您没有使用该格式,则必须声明所有内容。

$('#my-datatable').dataTable( {

    "sAjaxSource": "/url/here",


    "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "widget", "value": "token" } );

            request = $.ajax({
              "dataType": 'json', 
              "type": "GET", 
              "url": sSource, 
              "data": aoData, 
              "success": fnCallback
            });
      },


      "aoColumns": [
            { "mDataProp": "id"},
            { "mDataProp": "fizz"},
            { "mDataProp": "name"},
            { "mDataProp": "tag"},
            { "mDataProp": "tag"},
            { "mDataProp": "attributeValue.name"},
            { "mDataProp": "attributeValue.tag"},
        ],

    });

This is the format: http://datatables.net/release-datatables/examples/server_side/post.html

这是一种格式:http://datatables.net/release-datatables/examples/server_side/post.html

#2


1  

Try to enclose your JSON object with aaData like:

尝试将JSON对象与aaData类括起来:

{"aaData" : 

[{"id":1,"attributeType":{"id":1,"name":"test1","tag":"test-type","is-dog":false},"attributeValue":{"id":null,"name":"blah","tag":"BLAH"},"buzz":1,"fizz":"53abc"}]

}

#1


9  

Datatables requires a certain format for results. If you are not using that format, you have to declare everything.

datatable需要一个特定的结果格式。如果您没有使用该格式,则必须声明所有内容。

$('#my-datatable').dataTable( {

    "sAjaxSource": "/url/here",


    "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "widget", "value": "token" } );

            request = $.ajax({
              "dataType": 'json', 
              "type": "GET", 
              "url": sSource, 
              "data": aoData, 
              "success": fnCallback
            });
      },


      "aoColumns": [
            { "mDataProp": "id"},
            { "mDataProp": "fizz"},
            { "mDataProp": "name"},
            { "mDataProp": "tag"},
            { "mDataProp": "tag"},
            { "mDataProp": "attributeValue.name"},
            { "mDataProp": "attributeValue.tag"},
        ],

    });

This is the format: http://datatables.net/release-datatables/examples/server_side/post.html

这是一种格式:http://datatables.net/release-datatables/examples/server_side/post.html

#2


1  

Try to enclose your JSON object with aaData like:

尝试将JSON对象与aaData类括起来:

{"aaData" : 

[{"id":1,"attributeType":{"id":1,"name":"test1","tag":"test-type","is-dog":false},"attributeValue":{"id":null,"name":"blah","tag":"BLAH"},"buzz":1,"fizz":"53abc"}]

}