第一步:安装 django-cors-headers

pip install django-cors-headers

 

第二步:配置settings.py文件

-----------------------和前端配合指定可以跨域的用户----------------------------

from corsheaders.defaults import default_headers

CORS_ALLOW_HEADERS = default_headers + (
'Content-Type',
)
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True

------------------------------------

INSTALLED_APPS = [

  'corsheaders',

]

-----------------------------------

MIDDLEWARE = [

  'corsheaders.middleware.CorsMiddleware',  # 尽量往上放

  'django.middleware.common.CommonMiddleware',

]

================以下是前端请求在header里面添加的==================

headers:{'Content-Type': "application/x-www-form-urlencoded"}