如何将json等数据结构发布到烧瓶中?

Rob*_*bin 14 python post json flask

我有这样的数据结构:

在此输入图像描述

我试着通过$ .ajax将它发送到服务器:

$.ajax({
    type: 'POST',
    data: post_obj, //this is my json data
    dataType: 'json',
    url: '',
    success: function(e){
       console.log(e);
    }
});
Run Code Online (Sandbox Code Playgroud)

我希望通过烧瓶在服务器中获取它:title = request.form['title'] 工作正常!

但我怎么得到content

request.form.getlist('content') 不起作用.

这是firebug中的帖子数据:

在此输入图像描述

非常感谢:D

Aud*_*kas 17

您发送的数据编码为查询字符串而不是JSON.Flask能够处理JSON编码数据,因此发送它更有意义.这是您在客户端需要做的事情:

$.ajax({
    type: 'POST',
    // Provide correct Content-Type, so that Flask will know how to process it.
    contentType: 'application/json',
    // Encode your data as JSON.
    data: JSON.stringify(post_obj),
    // This is the type of data you're expecting back from the server.
    dataType: 'json',
    url: '/some/url',
    success: function (e) {
        console.log(e);
    }
});
Run Code Online (Sandbox Code Playgroud)

在服务器端,通过request.json(已解码)访问数据:

content = request.json['content']
Run Code Online (Sandbox Code Playgroud)