Brian Leach
Brian Leach

Reputation: 4154

dataTables flattens JSON data on POSTing, how to decode into python dictionary

I am using the jQuery dataTables plugin. When I POST a request for data to the server, dataTables provides lots of info regarding ordering, search values (if any), and column metadata.

Using the Chrome debugger, I see that the data ready to be sent in the POST request is a properly formed JSON object (snippet below from within the dataTables object)

"ajax": { // define ajax settings
    ...
    "type": "POST", // request type
    "timeout": 20000,
    "data": function(data) { // add request parameters before submit    
        $.each(ajaxParams, function(key, value) {
            data[key] = value;
        });
        var data = JSON.stringify(data); // here, the data is properly formed JSON. I tried stringifying it to no avail
    },

In my view on the server (using Python & Flask), I see the data comes through like below (I used a screenshot from PyCharm so you can see the details of how the data comes across).

enter image description here

So far, I have tried playing with the json and formencode modules on the server and tried to stringify the data before sending from the client, but nothing changes. Datatype on the server is a Werkzeug CombinedMultiDict, although I don't think that matters at this point.

Upvotes: 0

Views: 501

Answers (1)

Zoltan Fedor
Zoltan Fedor

Reputation: 2107

In Ajax set:

type: "POST",

data: data,

dataType: "json",

contentType: "application/json;charset=utf-8",

and then in flask just use:

import json

...

data = jsonify(request.get_json(force=True))

Upvotes: 1

Related Questions