2013-04-04 18 views
5

voglio passare un oggetto json che contiene oggetti nidificati dal mio client al mio server.come passare correttamente un oggetto json al server flask usando jquery ajax

sul lato client, la mia struttura dati assomiglia a questo:

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' 

e la mia chiamata AJAX è simile al seguente:

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

dopo la pubblicazione di questi dati al server, che viene eseguito utilizzando python, uso l'oggetto request.form per ispezionare ciò che è stato pubblicato dal client. Mi piacerebbe i dati ad essere strutturato allo stesso modo, tuttavia, questo è l'uscita sul 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')]) 

Come si può vedere, la risposta [ 'ospiti'] oggetto ottenuto appiattita, e tutti i suoi i bambini, come ad esempio:

'gli ospiti [2] [primo]'

... sono solo stringhe, non elementi della loro risposta genitore [ 'ospiti'].

c'è un modo migliore per inviare questo blocco di dati dal mio client al mio server e mantenere la sua struttura correttamente?

grazie!

risposta

11

Si potrebbe inviare il vostro oggetto come una stringa 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); 
}); 

e quindi utilizzare request.json per accedervi.

+0

Totalmente funziona! Grazie. Una cosa, il metodo .done() non viene mai eseguito, anche se riesco a vedere i dati sul mio server e quando controllo la scheda di rete di chrome dev, c'è un POST 200 OK per questa richiesta. – SeanPlusPlus

+0

Non importa il commento sopra. Devo jsonify la mia risposta dal server. Grazie ancora per l'aiuto con questo. – SeanPlusPlus

0

Sul lato client, è necessario convertire l'oggetto javascript in una stringa json. Per fare ciò, è possibile utilizzare questo:

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

Poi sul lato server, è necessario convertire tale oggetto ad un Dictionnary Python usando il modulo 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 ora è un Dictionnary pitone e puoi fare quello che vuoi con esso

Problemi correlati