首页 > 其他 > 详细

【Flask】解决跨域CORS

时间:2020-01-01 22:22:10      阅读:122      评论:0      收藏:0      [点我收藏+]
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS, cross_origin
from dataCities import cities

app = Flask(__name__)
CORS(app, supports_credentials=True)


@app.route("/v1/cities", methods=["GET", ‘OPTIONS‘])
def hello():
    get_city_type = request.args.get(‘type‘)
    #print(get_city_type)
    # print(cities)
    if get_city_type == ‘guess‘:
        data = cities[1]
    if get_city_type == ‘hot‘:
        data = cities[2]
    if get_city_type == ‘group‘:
        data = cities[1]
    #print(data)
    #print(type(data))
    return jsonify(data), 201, {‘Content-Type‘: ‘application/json‘}


@app.after_request
def af_request(resp):
    """
    #请求钩子,在所有的请求发生后执行,加入headers。
    :param resp:
    :return:
    """
    resp = make_response(resp)
    resp.headers[‘Access-Control-Allow-Origin‘] = ‘http://localhost:8080‘
    resp.headers[‘Access-Control-Allow-Methods‘] = ‘GET,POST‘
    resp.headers[‘Access-Control-Allow-Credentials‘] = ‘true‘
    resp.headers[‘Access-Control-Allow-Headers‘] = ‘x-requested-with,content-type‘
    return resp

 

【Flask】解决跨域CORS

原文:https://www.cnblogs.com/colipso/p/12130184.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!