zoukankan      html  css  js  c++  java
  • Django——跨域问题的解决

    Django 跨域问题的解决

    跨域介绍

    • 同源:
      • 指的是协议, 域名, 端口
      • 如果完全一样, 称为同源
    • 跨域:
      • 如果不一样, 称为跨域
      • 问题: 没有经过对方授权的时候, 不能去读取对方的资源

    跨域设置

    • 目的: 可以解决, 前端项目和后端项目的跨域问题

    • 官方文档:django-cors-headers

    • 操作流程:

      • 1, 安装
        pip install django-cors-headers
        
      • 2, 添加应用
        INSTALLED_APPS = [
            ...
            'corsheaders',
            ...
        ]
        
      • 3, 中间层设置
        MIDDLEWARE = [
            'corsheaders.middleware.CorsMiddleware' ,
            'django.middleware.common.CommonMiddleware'
            ...
        ]
        
      • 4, 添加白名单

        # CORS
        CORS_ORIGIN_WHITELIST = (
            '127.0.0.1:8080',  # 报错的话,在前面加上http://
            'localhost:8080',
            'www.meiduo.site:8080',
            'api.meiduo.site:8000'
        )
        CORS_ALLOW_CREDENTIALS = True  # 允许携带cookie
        

  • 相关阅读:
    Oracle第一课
    Web前端试题
    E
    Kingdom of Black and White
    D. Let's Go Hiking
    2021牛客寒假 第一场
    Codeforces Round #691 (Div. 2)
    CF1461D
    CF1461B
    浙财16th校赛 F因子
  • 原文地址:https://www.cnblogs.com/pywjh/p/14431255.html
Copyright © 2011-2022 走看看