08 Django REST Framework 解决前后端分离项目中的跨域问题

01-安装模块

pip install django-cors-headers

02-添加到INSTALL_APPS中

INSTALLED_APPS = (
    ...
    'corsheaders',
 ... )

03-添加中间件

'corsheaders.middleware.CorsMiddleware',

复制代码
MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    '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',
]
复制代码

04-设置为True

CORS_ORIGIN_ALLOW_ALL = True

 05-另一种解决方法 - 添加响应头

 

posted @   huangyanpeng  阅读(379)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示