How to properly transfer a json object to a flash server using jquery ajax

I want to pass a json object containing nested objects from my client to my server.

on the client side, my data structure is as follows:

var response = {};
response['screening'] = '1';
response['assistance'] = 'wheelchair access';
response['guests'] = {};
response['guests']['1'] = {}
response['guests']['1']['first'] = 'John'
response['guests']['1']['last'] = 'Smith'
response['guests']['2'] = {}
response['guests']['2']['first'] = 'Dave'
response['guests']['2']['last'] = 'Smith'

and my ajax call is as follows:

$.ajax({
  type: "POST",
  url: window.location.pathname,
  data: response
 }).done(function( msg ) {
   alert( "Data Saved: " + msg );
 });

after posting this data on my server, which starts using python flags, I use the request.form object to check what was sent by the client. I would like the data to be structured the same way, however this is the result on the server:

ImmutableMultiDict([('guests[1][first]', u'John'), ('screening', u'2'), ('guests[2][last]', u'Smith'), ('guests[2][first]', u'Dave'), ('assistance', u'wheelchair access'), ('guests[1][last]', u'Smith')])

as you can see, the response object ['guest'] was flattened, and all its children, for example:

'guests [2] [first]

... are just strings, not elements of the parent response ['guests'].

?

!

+5
2

JSON:

var data = {
    screening: '1',
    assistance: 'wheelchair access',
    guests: [
        {
            first: 'John',
            last: 'Smith'
        },
        {
            first: 'Dave',
            last: 'Smith'
        }
    ]
};

$.ajax({
    type: 'POST',
    url: window.location.href,
    data: JSON.stringify(response),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
}).done(function(msg) {
    alert("Data Saved: " + msg);
});

request.json .

+11

javascript json. :

JSON.stringify(my_object) // This will return a string that you can pass in you ajax request

dictionnary python json:

import simplejson
my_new_object = simplejson.loads(my_json) // my_json is my_object from the client (previously called my_object)

my_new_object python, , .

0

All Articles