使用sqlalchemy Datatable的INVALID JSON

时间:2016-03-24 13:24:34

标签: json ajax sqlalchemy datatables pyramid

我正在使用sqlalchemy Datatable示例中存在的相同模板和示例: http://sqlalchemy-datatables.readthedocs.org/en/latest/ 我的代码如下,我的问题是无效的json,我找不到任何问题。有帮助吗? rowTable.output_result()的输出是:

{'aaData':[{'1':'DOC - 1457715381','0':'60352794'},{'1':'DOC - 1457715381','0':'2768077336'},{ '1':'DOC - 1457715381','0':'6247239243'},{'1':'DOC - 1457715381','0':'8257884017'},{'1':'DOC - 1457715381', '0':'8508822379'}],'iTotalRecords':'5','sEcho':'1','iTotalDisplayRecords':'5'}

错误:DataTables警告:table id = mytable - 无效的JSON响应。有关此错误的详细信息,请参阅http://datatables.net/tn/1

MAKO:

@view_config(route_name='doclist', request_method='GET', renderer='service:/templates/partials/doclist.mako', permission = 'login')
def viewListDocuments(self):
r = self.r
creator_id = 18
columns = []
columns.append(ColumnDT('doccode'))
columns.append(ColumnDT('doctitle'))
query = DBSession.query(Document).filter(Document.creator_id == creator_id)
rowTable = DataTables(r.GET, Document, query, columns)
return rowTable.output_result()

HTML PAGE:

 <table id="mytable">
            <thead>
            <tr>
                <th>
                    doccode
                </th>
                <th>
                    doctitle
                </th>                    
            </tr>
            </thead>
            <tbody>
            </tbody>

        </table>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
    $('#mytable').dataTable({
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "${request.route_path('doclist')}"
    });
});

2 个答案:

答案 0 :(得分:0)

我认为问题是返回的JSON使用单引号而不是双引号。它应该是这样的:

{
    "aaData": [{
        "1": "DOC - 1457715381",
        "0": "60352794"
    }, {
        "1": "DOC - 1457715381",
        "0": "2768077336"
    }, {
        "1": "DOC - 1457715381",
        "0": "6247239243"
    }, {
        "1": "DOC - 1457715381",
        "0": "8257884017"
    }, {
        "1": "DOC - 1457715381",
        "0": "8508822379"
    }],
    "iTotalRecords": "5",
    "sEcho": "1",
    "iTotalDisplayRecords": "5"
}

您可以使用JSONlint测试您的JSON,http://datatables.net/tn/1在页面上提供,用于记录您的错误消息({{3}})

答案 1 :(得分:0)

我认为问题出在查询中。试试这个:

query = DBSession.query().select_from(Feature).filter(Document.creator_id == creator_id)

我认为您指出的文档未更新