如何在Python中获取JSON对象(Flask Framework)

如何在Python中获取JSON对象(Flask Framework)。 下面是我的代码片段

var hotel=$( "#listHotel option:selected" ).val(); if(hotel!="select") { $.ajax({ url: '/getHotels', data: {'hotel':hotel}, type: 'POST', success: function(response){ alert(response); var r= JSON.parse(response); var rating =r.message $("#rate").html("Ratings : "+rating); $("#rate").show('slow'); console.log(response); }, error: function(error){ alert(response); console.log(error); } }); }` 

如何在我的Python脚本Flask Framework获取JSON值hotel

 from flask import Flask, render_template, json, request app = Flask(__name__) @app.route('/') def main(): return render_template('index.html') @app.route('/getHotels',methods=['POST','GET']) def getHotels(): try: _hotel=request.POST['hotel'] print _hotel 

这是我在Python中的代码

在您的javascript中将数据转换为JSON并将contentType设置为"application/json"

 data: JSON.stringify({'hotel':hotel}), contentType : "application/json", 

在您的flask函数中使用request.json获取JSON:

 @app.route('/getHotels') def getHotels(): hotel = request.json['hotel'] .....