Ajax发布到Flask - json

时间:2016-05-17 15:51:00

标签: ajax flask

我在客户端进行了自动填充的谷歌地图查询 - 我试图转移&#34;地点&#34;一旦用户选择它就反对到服务器 - 我可以在客户端解析它,但我认为它更容易做服务器端。我通过浏览器&#39;网络&#39;验证了检查它是否正在发送正确的json对象,但服务器端我无法获得正确的对象。我已经尝试了每一个请求的排列。*我可以找到,或者得到无或我知道的<module 'flask.json'>是不对的。

代码:

function sendplace() {
    $('placebutton').click(function() {
        var add1 = place;
        console.log(place);
        $.ajax({
            url: '/new_place2',
            data: JSON.stringify(place),
            contentType: 'application/json;charset=UTF-8',
            type: 'POST',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }

和服务器端:

@app.route('/new_place2', methods=['GET', 'POST'])
def new_place2():
    place = request.form.get('place')
    print "address: ", place
    return ("Success, (*&)er!")

1 个答案:

答案 0 :(得分:3)

您应该使用request.get_json()。 request.form保留给mimetype application/www-form-urlencoded。如果get_json()无效,请查看request.data并确保您可以在其上运行以下内容:

import json

data = json.loads(request.data)
data.get('place')

如果这不起作用,您的JSON可能有问题

相关问题