首页 > 解决方案 > CORS 标头不适用于 Django 后端 - Angular 前端

问题描述

django-cors-headers我通过安装并按照中提到的步骤在我的 Django 后端实现了 CORS https://github.com/OttoYiu/django-cors-headers。本质上,我执行了以下步骤-

  1. pip install django-cors-headers
  2. INSTALLED_APPS = ( ... 'corsheaders', ... )
  3. MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ]

  4. CORS_ORIGIN_WHITELIST = ( 'localhost:8000', #LB '10.254.138.226:443' )

当我使用我的角度前端时,我仍然无法让它工作。例如,我的 POST 请求变成了 OPTIONS。

zone.js:2935 OPTIONS https://10.254.138.226/api/users 0 () error is ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …}

当我使用邮递员时,我没有这个问题。有人可以帮忙吗?

为了提供更多上下文 - 我在两台不同的机器上运行前端和后端 (DRF)。事实上,我有 2 个后端实例在两台不同的机器上运行,并且我有一个负载均衡器,将请求从<LB IP>:443一个<Backend IP>:8000基本 URL 映射到一个。

标签: djangoangulardjango-rest-frameworkcorsoptions

解决方案


我可能永远不会找到它为什么会发生的确切问题。我刚刚删除并重新创建了 LB,一切都开始自动运行。


推荐阅读