报错信息:
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问题排除