- pip install django-cors-headers
- 在settings.py文件中添加如下
INSTALLED_APPS = [
'corsheaders',
]
MIDDLEWARE = [
# 需要添加在CommonMiddleware中间件之前
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
- 添加白名单
# CORS_ORIGIN_ALLOW_ALL为True, 指定所有域名(ip)都可以访问后端接口, 默认为False
CORS_ORIGIN_ALLOW_ALL = True
# CORS_ORIGIN_WHITELIST指定能够访问后端接口的ip或域名列表
# CORS_ORIGIN_WHITELIST = [
# "http://127.0.0.1:8080",
# "http://localhost:8080",
# "http://192.168.1.63:8080",
# "http://127.0.0.1:9000",
# "http://localhost:9000",
# ]
# 允许跨域时携带Cookie, 默认为False
CORS_ALLOW_CREDENTIALS = True