简体   繁体   中英

CORS policy: Response to preflight request doesn't pass access control

Here I'm trying to run this code but i am getting CORS error when integrated with angular. this is my source code Settings.py file

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webapp',
'rest_framework',
'knox',
'django_rest_passwordreset',
'corsheaders',
  ]
MIDDLEWARE = [
'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',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
 ]
ALLOWED_HOSTS=['*']
CORS_ORIGIN_ALLOW_ALL = True

I'm adding all of these but I'm getting same error while running. Tell me the answer

I think it's because, you need to add Allowed header s. Because when you're calling api, by default Content-Type will be added. Add this at the end.

CORS_ALLOW_HEADERS = ('Authorization', 'Content-Type', 'Cache-Control', 'X-Requested-With')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM