如何在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值酒店
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']
.....
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。