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

报错信息:

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问题排除

猜你喜欢

转载自blog.csdn.net/qq_29974229/article/details/125561608