python flask 接口

from flask import Flask,jsonify,request

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False #设置接口数据编码
#数据格式转换
def set_json(data):
    return jsonify(data)
#设置跨域访问
@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,session_id')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS,HEAD')
    # 这里不能使用add方法,否则会出现 The 'Access-Control-Allow-Origin' header contains multiple values 的问题
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response
@app.route('/')
def hello_world():
    code= request.args.get("code")
     post= request.form.get("data")
    data={
        'code':1,
        'msg': "成功",
        'data':{
            "user":"jin"
        }

    }
    return set_json(data)


if __name__ == '__main__':
    app.run()

评论

刷新

友情链接