美文网首页
django跨域请求详解

django跨域请求详解

作者: LittleBear_6c91 | 来源:发表于2019-05-18 14:23 被阅读0次

    不行就把方法1,2都加进去

    方法一

    django允许跨域请求配置
    下载corsheader

    pip install django-cors-headers
    

    修改settings.py中配置
    在INSTALLED_APPS中增加corsheaders

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        
        'corsheaders',
    ]
    
    MIDDLEWARE_CLASSES = [
        '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.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    

    方法二(在settings.py最下面加)

    # 跨域增加忽略
    CORS_ALLOW_CREDENTIALS = True
    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ORIGIN_WHITELIST = ('*')#这句话可以不加白名单报错就不加!!!!!!!!!!!!!!
    CORS_ALLOW_METHODS = (
        '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 = ['*']
    
    

    相关文章

      网友评论

          本文标题:django跨域请求详解

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