- 先下载对应的包
pip install flask_cors
- 导入包
from flask_cors import CORS
- 创建app后中写入
CORS(app, resources=r'/*')
示例代码如下:
1 from flask import Flask 2 from flask_cors import CORS 3 from flask_mail import Mail 4 from config import APP_ENV, config,API_VERSION 5 6 mail = Mail() 7 8 def create_app(): 9 """ 10 工厂函数,创建APP实例 11 :return app实例 12 """ 13 setupLogging(config[APP_ENV].LOGGING_LEVEL) 14 15 app = Flask(__name__) 16 app.config.from_object(config[APP_ENV]) 17 18 CORS(app, resources=r'/*') # r'/*' 是通配符,让本服务器所有的URL 都允许跨域请求 19 20 app.after_request(after_request) 21 22 # 创建Redis数据库连接对象 23 global redis_conn 24 redis_conn = redis.StrictRedis(host=config[APP_ENV].REDIS_HOST, port=config[APP_ENV].REDIS_PORT)