0
votes

I am using the same template and example which exists in sqlalchemy Datatable sample in: http://sqlalchemy-datatables.readthedocs.org/en/latest/ my code is the following, my problem is invalid json, that I could not find any problems in it. any helps? The output of rowTable.output_result() is:

{'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'}

ERROR: DataTables warning: table id=mytable - Invalid JSON response. For more information about this error, please see 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

2 Answers

0
votes

I think the problem is that the returning JSON uses single quotes instead of double quotes. It should read like this:

{
    "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"
}

You can test out your JSON using JSONlint, which is provided as a link on the page which documents your error message (http://datatables.net/tn/1)

0
votes

I think the problem is in the query. Try this:

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

I think that the documentation that you pointed out is not updated