美文网首页
Django 允许跨域请求

Django 允许跨域请求

作者: 徐德东 | 来源:发表于2018-02-02 19:12 被阅读0次

1.安装django-cors-headers

pip install django-cors-headers

2.配置settings.py文件

INSTALLED_APPS = [

    ...

    'corsheaders',

    ...

]

MIDDLEWARE_CLASSES = (

    ...

    'corsheaders.middleware.CorsMiddleware',

    'django.middleware.common.CommonMiddleware', # 注意顺序    ...

)

#跨域增加忽略

CORS_ALLOW_CREDENTIALS = True

CORS_ORIGIN_ALLOW_ALL = True

CORS_ORIGIN_WHITELIST = ('*')

CORS_ALLOW_METHODS = (

    'DELETE',

    'GET',

    'OPTIONS',

    'PATCH',

    'POST',

    'PUT',

    'VIEW',

)

# * 接受请求头。默认的如下

default_headers = (

'accept',

    'accept-encoding',

    'authorization',

    'content-type',

    'dnt',

    'origin',

    'user-agent',

    'x-csrftoken',

    'x-requested-with',

)

所以  CORS_ALLOW_HEADERS 可以不用定义

相关文章

网友评论

      本文标题:Django 允许跨域请求

      本文链接:https://www.haomeiwen.com/subject/hygezxtx.html