简体   繁体   English

如何使用jquery ajax将json对象正确传递给flask服务器

[英]how to correctly pass a json object to flask server using jquery ajax

i want to pass a json object which contains nested objects from my client to my server. 我想传递一个json对象,其中包含从我的客户端到我的服务器的嵌套对象。

on the client side, my data structure looks like this: 在客户端,我的数据结构如下所示:

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 looks like this: 我的ajax调用看起来像这样:

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

after posting this data to my server, which is run using python flask, i use the request.form object to inspect what was posted from the client. 在将此数据发布到使用python flask运行的服务器之后,我使用request.form对象来检查从客户端发布的内容。 i'd like the data to be structured the same way, however, this is the output 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['guests'] object got flattened, and all of its children, such as: 正如您所看到的,响应['guests']对象变得扁平化,并且它的所有子节点,例如:

'guests[2][first]' 的客人[2] [第一]'

... are just a strings, not elements of their parent response['guests']. ......只是一个字符串,而不是父母反应的元素['来宾']。

is there a better way to send this block of data from my client to my server, and maintain its structure properly? 有没有更好的方法将这个数据块从我的客户端发送到我的服务器,并保持其结构正确?

thanks! 谢谢!

You could send your object as a JSON string: 您可以将对象作为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);
});

And then use request.json to access it. 然后使用request.json访问它。

On the client side, you need to convert that javascript object to a json string. 在客户端,您需要将该javascript对象转换为json字符串。 To do so, you can use this: 为此,您可以使用:

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

Then on the server side, you need to convert that object to a python dictionnary using the json module: 然后在服务器端,您需要使用json模块将该对象转换为python dictionnary:

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

my_new_object is now a python dictionnary, and you can do whatever you want with it my_new_object现在是一个python dictionnary,你可以随心所欲地做任何事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM