aData is undefined

本文讨论了在Django应用中使用jQuery DataTables进行AJAX源数据加载时遇到的aData is undefined错误,并提供了解决方法。通过调整JSON响应格式和回调函数参数,可以确保数据正确呈现。

 

 

 

{"aaData": [["blah1", "blah2", "blah3"]]}

 

 

详情看;:

I try to implement jquery datatable ajax-source usage in Django however have some problems. The ajax call is working great and get the response however after that aData is undefined error is raised in javascript.

What would be the problem ?

These are what I have so far.

views.py

def model_history(request):
    o = Model.objects.get(id=request.GET["pk"])
    items_list = list(o.lastupdate_set.all().values())
    return HttpResponse(simplejson.dumps(items_list),'application/json')

js

    function viewModelHistory(pk){
        url1 = "/model/history/?pk="+pk;

        $('#history').dataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": url1,
            "fnServerData": function ( sSource, aoData, fnCallback ) {
              $.ajax( {
                "dataType": 'json',
                "type": "POST",
                "url": sSource,
                "data": aoData,
                "success": fnCallback
              } );
            }
          } );
}

listmodel.html

<table id="history">
                    <thead>
                        <tr>
                            <th>col1</th>
                            <th>col2</th>
                            <th>col3</th>
                            <th>col4</th>
                            <th>col5</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
share improve this question
 
   
What does fnCallback do and what parameters does it expect? –  ilvar Apr 30 '12 at 4:31

2 Answers

It looks like you have an issue with the format of your JSON structure. jquery.datatables expects it to look something like

{"aaData": [["blah1", "blah2", "blah3"]]}

If your JSON looks like

[["blah1", "blah2", "blah3"]]

In your view.py you need to wrap your item_list in a dictionary like so:

items_list_dict = {}
items_list_dict.update({'aaData': items_list})
return HttpResponse(simplejson.dumps(items_list_dict),'application/json')
share improve this answer
 

It will be helpful to post your JSON output, but I assume it looks like this:

[["key":"val"],["key":"val"]]

if so you need to remove your keys from the json. Datatables expects json like this:

[["val"],["val"]]

One thing you can do is make a list of list from your query

my_json = []
for item_list in items_list:
    a = [item_list.xxx_column_name_xxx]
    my_json.append(a)

items_list_dict = {}
items_list_dict.update({'aaData': my_json})
data = json.dumps(items_list_dict)

return HttpResponse(data, mimetype='application/json')
share improve this answer
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

混进IT圈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值