• 通过django中间件进行添加跨域头

1. 安装一个django cor包

pip install django-cors-headers

2. 在 项目/setting.py中, 新增

INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
 ]
MIDDLEWARE = (
    ...
    'corsheaders.middleware.CorsMiddleware',
   # cors-headers的中间件CorsMiddleware在注册时必须放在django-common中间件的前一个
    'django.middleware.common.CommonMiddleware', 
    ...
)

3. 添加白名单:在白名单内的所有域名都可以访问

# CORS
CORS_ORIGIN_WHITELIST = ( 'http:127.0.0.1:8080', 'http:localhost:8080', 'www.xxx.site:8080',
)
CORS_ALLOW_CREDENTIALS = True  # 允许携带cookie

相关文章:

  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-01-07
  • 2021-12-29
  • 2021-10-11
  • 2021-11-28
  • 2021-07-02
猜你喜欢
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2022-03-02
相关资源
相似解决方案