程序员

django中配置允许跨域请求

2019-06-19  本文已影响0人  pillows

安装django-cors-headers

pip install django-cors-headers

配置settings.py文件

INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
 ]
MIDDLEWARE_CLASSES = (
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
)
# 跨域问题
CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_CREDENTIALS = True

CORS_ORIGIN_WHITELIST = (
    '*',
)
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)
CORS_ALLOW_HEADERS = [
    '*',
    'dnt',
    'source',
    'origin',
    'Pragma',
    'accept',
    'user-agent',
    'x-csrftoken',
    'X_FILENAME',
    'content-type',
    'authorization',
    'XMLHttpRequest',
    'accept-encoding',
    "x-requested-with",
]
上一篇下一篇

猜你喜欢

热点阅读