欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

aData is undefined

程序员文章站 2022-07-16 17:22:59
...

 

 

 

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

 

 

详情看;:

aData is undefined
            
    
    博客分类: datatable aDatadatatable 

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>
shareimprove 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')
shareimprove this answer
 
aData is undefined
            
    
    博客分类: datatable aDatadatatable aData is undefined
            
    
    博客分类: datatable aDatadatatable 

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')
shareimprove this answer
相关标签: aData datatable