安装django-cors-headers pip install django-cors-headers
1 配置settings.py文件: 2 INSTALLED_APPS = [ 3 ... 4 'corsheaders', 5 ... 6 ] 7 8 MIDDLEWARE_CLASSES = ( 9 ... 10 'corsheaders.middleware.CorsMiddleware', 11 'django.middleware.common.CommonMiddleware', # 注意顺序 12 ... 13 ) 14 15 16 #跨域增加忽略 17 CORS_ALLOW_CREDENTIALS = True 18 CORS_ORIGIN_ALLOW_ALL = True 19 CORS_ORIGIN_WHITELIST = ( 20 21 ) 22 23 CORS_ALLOW_METHODS = ( 24 'DELETE', 25 'GET', 26 'OPTIONS', 27 'PATCH', 28 'POST', 29 'PUT', 30 'VIEW', 31 ) 32 33 CORS_ALLOW_HEADERS = ( 34 'XMLHttpRequest', 35 'X_FILENAME', 36 'accept-encoding', 37 'authorization', 38 'content-type', 39 'dnt', 40 'origin', 41 'user-agent', 42 'x-csrftoken', 43 'x-requested-with', 44 'Pragma', 45 )