如何在Python(Flask Framework)中获取JSON对象。以下是我的代码段`
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); } }); }`
如何hotel在我的Python脚本中获取JSON值Flask Framework
hotel
Flask Framework
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":
contentType
"application/json"
data: JSON.stringify({'hotel':hotel}), contentType : "application/json",
在flask函数中,使用来获取JSON request.json:
request.json
@app.route('/getHotels') def getHotels(): hotel = request.json['hotel'] .....