程序go 2016-05-17 15:50 采纳率: 100%
浏览 9

Ajax发布到Flask-json [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/20001229/how-to-get-posted-json-in-flask" dir="ltr">How to get POSTed JSON in Flask?</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-05-17 16:22:30Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I've got a google-maps lookup with autocomplete on the client side - I'm trying to transfer the "place" object over to the server once the user selects it - I could parse it down client-side, but I figured it's easier to do server-side. I verified from the browser 'network' inspect that it is sending over the correct json object, but server side I can't get the right object. I've tried every permutation of request.* that I can find and either get None or a <module 'flask.json'> which I know isn't right.

Code:

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

and server-side:

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

1条回答 默认 最新

  • weixin_33697898 2016-05-17 16:09
    关注

    You should use request.get_json(). request.form is reserved for the mimetype application/www-form-urlencoded. If get_json() doesn't work look at request.data and ensure that you can run the following on it:

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

    If that doesn't work you probably have a problem with your JSON

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度