如何发布数据结构像JSON到烧瓶? [英] How to post data structure like json to flask?

查看:170
本文介绍了如何发布数据结构像JSON到烧瓶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据结构:


我尝试通过$ .ajax发送给服务器:



<$ p
$ type $'$'$'$'$ $ $ $ $ $ $ $ $ $'$'$'$'$'$'$'$'$'$' b $ b url:'',
success:function(e){
console.log(e);
}
});

我想在服务器上通过flask获得它: title = request.form ['title'] 工作正常!



但是我如何得到 content ?



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

这是firebug中的发布数据: b
$ b


非常感谢:D

解决方案

您正在将您的数据编码为查询字符串而不是JSON。 Flask能够处理JSON编码的数据,所以像这样发送更有意义。以下是您需要在客户端执行的操作:

$。ajax({
类型:'POST',
//提供正确的Content-Type,以便Flask知道如何处理它。
contentType:'application / json',
//编码你的数据作为JSON。
data:JSON.stringify(post_obj),
//这是您期望从服务器返回的数据类型。
dataType:'json',
url:'/ some / url',
success:function(e){
console.log(e);
}
});

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

  content = request.json ['content'] 


I have a data structure like this:

I'm try to send it to server by $.ajax:

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

and I want get it in server by flask: title = request.form['title'] working fine!

But how do I get content ?

request.form.getlist('content') doesn't work.

This is the post data in firebug:

Thanks a lot :D

解决方案

You are sending your data encoded as query string instead of JSON. Flask is capable of processing JSON encoded data, so it makes more sense to send it like that. Here's what you need to do on the client side:

$.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);
    }
});

On the server side data is accessed via request.json (already decoded):

content = request.json['content']

这篇关于如何发布数据结构像JSON到烧瓶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆