• Django解决跨域问题(django-cors-headers)


    1. 安装 django-cors-headers

    2. 修改setting.py

    INSTALLED_APPS = [
        ...
        'corsheaders'# 放在新建的其他项目之前
        ...
     ] 
    
    # 添加中间件
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',# 默认
        'django.contrib.sessions.middleware.SessionMiddleware', # 默认
    
        'corsheaders.middleware.CorsMiddleware',# 默认
        # 注意顺序,即在上一个的下面
        'django.middleware.common.CommonMiddleware', # 新增 ✔
    
        'django.middleware.csrf.CsrfViewMiddleware',# 默认
        'django.contrib.auth.middleware.AuthenticationMiddleware',# 默认
        'django.contrib.messages.middleware.MessageMiddleware', # 默认
        'django.middleware.clickjacking.XFrameOptionsMiddleware',# 默认
        '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',
    )
    CORS_ALLOW_HEADERS = (
        'XMLHttpRequest',
        'X_FILENAME',
        'accept-encoding',
        'authorization',
        'content-type',
        'dnt',
        'origin',
        'user-agent',
        'x-csrftoken',
        'x-requested-with',
    )
    #部署到云服务上必备
    ALLOWED_HOSTS = ['*']
  • 相关阅读:
    bzoj 4610 Ceiling Functi
    uva 01350
    uva 12075
    uva 01393
    uva 11038
    CF 496E
    CF 496D
    poj 3167
    hdu 4622
    spoj 7258
  • 原文地址:https://www.cnblogs.com/HZY258/p/11926609.html
Copyright © 2020-2023  润新知