为什么jQuery数据表不能解析我的JSON? [英] Why can't jQuery dataTables parse my JSON?

查看:96
本文介绍了为什么jQuery数据表不能解析我的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试填写 dataTable ,如下所示:



dataTable({
sAjaxSource:/ someURLOnMyServer,
bDestroy:true,
fnServerParams:function(serverParams){
serverParams.push(
{
name:widget,
value:token
}
);
}
});

而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>

根据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
}
]

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

  TypeError:aData未定义
[Break On这个错误]

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

任何人都可以发现出了什么问题?或者我没有正确设置我的 dataTable 对象,或者JSON回来与其正在尝试填充的HTML表的模式不匹配。无论哪种方式,我都迷路了感谢提前!

解决方案

数据资料需要一定的格式才能得到结果。如果您不使用该格式,则必须声明所有内容。

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

sAjaxSource / url / here,


fnServerData:function(sSource,aoData,fnCallback){
aoData.push({name:widget :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},
],

});

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


I am trying to populate a dataTable as follows:

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

And the HTML table it is populating:

<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:

[
   {
      "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:

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!

解决方案

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

$('#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

这篇关于为什么jQuery数据表不能解析我的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆