【问题标题】:How can I get the Posted JSON object in Python Flask App [duplicate]如何在 Python Flask App 中获取 Posted JSON 对象 [重复]
【发布时间】:2015-10-05 23:06:37
【问题描述】:

我正在开发一个基于 Python 框架 Flask 的简单网络应用程序。在这里,我试图从 POST AJAX 请求中获取 JSON 响应。下面的代码显示了我的 AJAX 调用:

var hotel=$( "#listHotel option:selected" ).val();
$.ajax({
    url: "/getHotels",
    contentType: "application/xml; charset=utf-8",
    data: JSON.stringify({'hotel':hotel}),
    type: "POST",
    success: function(response){
        var r= JSON.parse(response);
        var rating =r.message
        alert(rating);
        $("#rate").html("Ratings : "+rating);
        $("#rate").show('slow');
        console.log(response);
    },
    error: function(error){
        alert(response);
        console.log(error);
    }
});

这是我的 Python 代码

def getHotels():
    try:
        _hotel = {"value": request.json['hotel']}
        print _hotel
        hotel= _hotel["value"]
        print hotel

但我没有得到 JSON 对象。我已经尽力了。请帮我获取 JSON 对象/值。

【问题讨论】:

    标签: jquery python ajax json flask


    【解决方案1】:

    由于您只是在 ajax 请求中传递了一个值,因此您可以替换该行

    data: JSON.stringify({'hotel':hotel}),
    

    通过

    data: {'hotel': hotel},
    

    从那里,您可以通过以下方式访问烧瓶中的值

    request.form['hotel']
    

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 2015-10-05
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多