1.安装 django-cors-headers
pip install django-cors-headers
2.修改 setting.py
INSTALLED_APPS = [
...
‘corsheaders‘,
...
]
# 添加中间件
# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (‘*‘) #可加可不加
CORS_ALLOW_METHODS = (
‘DELETE‘,
‘GET‘,
‘OPTIONS‘,
‘PATCH‘,
‘POST‘,
‘PUT‘,
‘VIEW‘,
)
CORS_ALLOW_HEADERS = (
‘XMLHttpRequest‘,
‘X_FILENAME‘,
‘accept-encoding‘,
‘authorization‘,
‘content-type‘,
‘dnt‘,
‘origin‘,
‘user-agent‘,
‘x-csrftoken‘,
‘x-requested-with‘,
)
#部署到云服务上必备
ALLOWED_HOSTS = [‘*‘]
原文:https://www.cnblogs.com/antique/p/10903677.html