其他分享
首页 > 其他分享> > django2.0.4跨域完美解决办法

django2.0.4跨域完美解决办法

作者:互联网

1.安装 django-cors-headers

pip install  django-cors-headers

 

 

 

2.修改 setting.py

 

INSTALLED_APPS = [
...
'corsheaders',
...
]

# 添加中间件

MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware',# 新增 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

# 跨域增加忽略
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 = ['*']

标签:解决办法,corsheaders,跨域,django2.0,middleware,django,ALLOW,CORS,contrib
来源: https://www.cnblogs.com/1488boss/p/10889482.html