Django設置容許跨域請求

1. 安裝模塊django-cors-headers

pip3 install django-cors-headers

2. 配置django項目的settings.py文件

配置INSTALLED_APPSpython

INSTALLED_APPS = [
    ...,
    'corsheaders'
]

配置中間件, 注意順序shell

MIDDLEWARE = [
    ...,
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware'
]

再settings.py文件末尾添加下面內容django

'''
跨域設置
'''
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)
相關文章
相關標籤/搜索