Django跨域请求的坑-django-cors-headers

版权声明:非诚勿扰 https://blog.csdn.net/kaikai0803/article/details/84887856

Django跨域请求的坑

解决办法:django-cors-headers
django-cors-headers GitHub地址

简要过程

1 pip安装

pip install django-cors-headers

以下内容在settings文件中

2 注册进app

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

3 注册进中间件

MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

4 设置允许访问

CORS_ORIGIN_ALLOW_ALL = True	# 所有人

或者

# 指定白名单
CORS_ORIGIN_WHITELIST = (
    'google.com',
    'hostname.example.com',
    'localhost:8000',
    '127.0.0.1:9000'
)

猜你喜欢

转载自blog.csdn.net/kaikai0803/article/details/84887856