Django跨域问题解决

安装 django-cors-headers

pip install django-cors-headers
• 1

修改配置

# 注册应用
INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)
# 中间件
MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ...
]
# CORS白名单
CORS_ORIGIN_WHITELIST = (
    '127.0.0.1:8080',
    'localhost:8080',
    'www.xxxx.com:8080',
    'api.xxxx.com:8000'
)
CORS_ALLOW_CREDENTIALS = True  # 允许携带cookie
ALLOWED_HOSTS = [
    'www.xxxx.com:8080',
    'api.xxxx.com:8000',
    '127.0.0.1'
]