CORS跨域处理
- 由于静态服务器(端口5000)需要向Flask启动的测试服务器(端口8000)发请求, 所以需要进行跨域配置
- Flask组件flask-cors可以处理跨域问题, 安装 pip install flask-cors
- 在app初始化文件的register_extensions函数中对跨域组件初始化
# app/__init__.py
...
from flask_cors import CORS
...
def register_extensions(app):
"""组件初始化"""
...
# 跨域组件初始化
CORS(app, supports_credentials=True) # 设置supports_credentials=True, 则允许跨域传输cookie
也可以在配置文件中指定要限制的域名
# app/settings/config.py
class DefaultConfig:
"""默认配置"""
...
# CORS
# CORS_ORIGINS = ['http://127.0.0.1:5000'] # 限定允许访问的域名, 不设置则全部允许