[Flask] to solve cross-domain CORS

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):
     "" " 
    # hook request, the request occurs after execution of all, addition of headers. 
    : Param RESP: 
    : return: 
    " "" 
    RESP = make_response (RESP) 
    resp.headers [ ' --Control-the Allow Access Origin ' ] = ' HTTP: // localhost: 8080 ' 
    resp.headers [ ' Access-Control-the Allow-Methods ' ] = ' the GET, the POST ' 
    resp.headers [ ' Access-Control-allow- Credentials ' ] = ' to true '
    resp.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
    return resp

 

Guess you like

Origin www.cnblogs.com/colipso/p/12130184.html