1. 安装跨域包
windown 10 :
1 pip install django-cors-headers
2.把下载的包引入app中
1 INSTALLED_APPS = [ 2 ...... 3 'corsheaders' 4 ]
3. 添加中间件
1 MIDDLEWARE = [ 2 'django.middleware.security.SecurityMiddleware', 3 'django.contrib.sessions.middleware.SessionMiddleware', 4 # 跨域访问,中间件是有顺序的 5 'corsheaders.middleware.CorsMiddleware', 6 'django.middleware.common.CommonMiddleware', 7 'django.middleware.csrf.CsrfViewMiddleware', 8 'django.contrib.auth.middleware.AuthenticationMiddleware', 9 'django.middleware.clickjacking.XFrameOptionsMiddleware', 10 ]
4.添加跨域配置信息
1 CORS_ALLOW_CREDENTIALS = True 2 CORS_ORIGIN_ALLOW_ALL = True 3 CORS_ORIGIN_WHITELIST = () 4 5 CORS_ALLOW_METHODS = ( 6 'DELETE', 7 'GET', 8 'OPTIONS', 9 'PATCH', 10 'POST', 11 'PUT', 12 'VIEW', 13 ) 14 15 CORS_ALLOW_HEADERS = ( 16 'XMLHttpRequest', 17 'X_FILENAME', 18 'accept-encoding', 19 'authorization', 20 'content-type', 21 'dnt', 22 'origin', 23 'user-agent', 24 'x-csrftoken', 25 'x-requested-with', 26 )