8

I am having some issues in processing the parameters sent by jquery datatables 1.10 when server side processing is enabled. I initialized the datatable in the javascript side like this:

var table = $('#mytable').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": {
                    'url': url,
                    'type': 'POST'
                },
                "columns": data
            } );

And receive the POST request in the Flask based server using this:

@app.route('/data/<data_key>', methods=['POST'])
def get_data(data_key):
    print request.form

    # do some processing here in order to filter data
    # ...

    return Response(json.dumps(data), status=200, mimetype='application/json')

In order to filter the data I tried to look inside request.form but the result is weird and can't be transformed easily to an array of objects. I get somthing like this:

ImmutableMultiDict(
[
('columns[0][data]', u'ReportDate'), 
('draw', u'1'),
('columns[1][name]', u''), 
('columns[1][data]', u'FundName'),
('columns[0][orderable]', u'true'), 
('columns[1][searchable]', u'true'), 
('columns[1][orderable]', u'true'), 
('order[0][column]', u'0'), 
('columns[0][name]', u''), 
('order[0][dir]', u'asc'), 
('search[value]', u''), 
('columns[1][search][regex]', u'false'), 
('columns[0][search][value]', u''), 
('search[regex]', u'false'), 
('columns[1][search][value]', u''), 
('columns[0][search][regex]', u'false'), 
('start', u'0'), 
('length', u'10'), 
('columns[0][searchable]', u'true')
]
)

In the jquery datatables documentation they say:

The order[i] and columns[i] parameters that are sent to the server are arrays of information:

order[i] - is an array defining how many columns are being ordered upon - i.e. if the array length is 1, then a single column sort is being performed, otherwise a multi-column sort is being performed.

columns[i] - an array defining all columns in the table.

In both cases, i is an integer which will change to indicate the array value. In most modern server-side scripting environments this data will automatically be available to you as an array.

However, Flask provide this data as a simple dictionary, is there a way to transform it to an array of objects easily?

2
  • 1
    Have you tried request.get_json()? More info here: flask.pocoo.org/docs/api/#flask.Request.get_json Commented Jul 25, 2014 at 20:36
  • 1
    At first it gives an empty result, I then tried request.get_json(force=True) and then I get '400: Bad Request'. For the moment, I hooked the javascript code to send the data as a json payload. Commented Jul 25, 2014 at 21:52

2 Answers 2

12

Get DataTable to send json

ajax: {
    url: "/api/data_table",
    type: "POST",
    data: function ( args ) {
      return { "args": JSON.stringify( args ) };
    }
},

In flask, parse the json

args = json.loads(request.values.get("args"))
columns = args.get("columns")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. For the record, the "Submit data as JSON in the request body" part is provided here in the docs.
1

You can use this little nifty package that I found https://github.com/bernii/querystring-parser

from querystring_parser import parser
args = parser.parse(request.query_string)
print args['columns']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.