zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Django报错:No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

djangoOn 报错 The is No access Control
2023-09-14 09:15:20 时间

报错信息:

Access to XMLHttpRequest at 'http://127.0.0.1:8081/api/login/' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

在这里插入图片描述

处理方法:

1. 安装django-cors-headers

pip install django-cors-headers

在这里插入图片描述

2. 修改settings.py

2.1 修改 INSTALLED_APPS

在这里插入图片描述

'corsheaders',

2.2 修改MIDDLEWARE

'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',

在这里插入图片描述

2.3 追加2处

在这里插入图片描述

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True

至此Access-Control-Allow-Origin问题排除