解决办法:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'yshop.middleware.AuthorizeMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# CSRF_COOKIE_HTTPONLY = True
CORS_ALLOWED_ORIGINS = [
'http://127.0.0.1:8080', # 允许跨域请求的源
]
CORS_ORIGIN_WHITELIST = [
'http://127.0.0.1:8080',
]
CORS_ALLOW_METHODS = [
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
]
CORS_ALLOW_HEADERS = [
'username', # 允许的请求头
'Authorization',
'*',
'dnt',
'source',
'origin',
'Pragma',
'accept',
'user-agent',
'x-csrftoken',
'X_FILENAME',
'content-type',
'authorization',
'authentication',
'XMLHttpRequest',
'accept-encoding',
"x-requested-with",
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)
}
# 支持跨域配置开始
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
最重要的一点:
中间件的顺序:
'corsheaders.middleware.CorsMiddleware', 必须放第一个!!!
标签:vue,跨域,middleware,django,ALLOW,CORS,contrib From: https://www.cnblogs.com/lytcreate/p/17618067.html