如何获得JSON对象在Python(瓶框架) [英] How to get a JSON Object in Python (Flask Framework)

查看:121
本文介绍了如何获得JSON对象在Python(瓶框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python(瓶框架)的JSON对象。 下面是我的code段 `

How to get a JSON Object in Python (Flask Framework). Below is my code snippet `

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

我怎样才能获得JSON值酒店在我的Python脚本瓶框架

How can I get the JSON value hotel in my Python script 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

这是我的code在Python

this is my code in Python

推荐答案

在你的JavaScript转换数据,以JSON和设置的contentType 应用/ JSON的

In your javascript transform the data to JSON and set the contentType to "application/json":

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

在你的烧瓶功能使用获得JSON request.json

In your flask function get the JSON using request.json:

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

这篇关于如何获得JSON对象在Python(瓶框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆