当前位置: 代码迷 >> 综合 >> 使用django-cors-headers来解决跨域问题
  详细解决方案

使用django-cors-headers来解决跨域问题

热度:113   发布时间:2023-10-12 12:56:38.0

在settting里面这样配置:


ALLOWED_HOSTS = ["*"]# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','graphene_django','corsheaders',  # 在这里加上
]
MIDDLEWARE = ['corsheaders.middleware.CorsMiddleware',  # 在这里加上'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','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
SESSION_COOKIE_HTTPONLY = False
SESSION_COOKIE_SAMESITE = NoneCORS_ALLOW_METHODS = ('DELETE','GET','OPTIONS','PATCH','POST','PUT','VIEW',
)CORS_ALLOW_HEADERS = ('XMLHttpRequest','X_FILENAME','accept-language','accept-encoding','authorization','content-type','dnt','origin','user-agent','x-csrftoken','x-requested-with','Pragma',
)