CORS标头不适用于Django后端-Angular前端 [英] CORS header not working for Django backend- Angular frontend
问题描述
我通过安装django-cors-headers
并遵循https://github.com/OttoYiu/django-cors-headers
中提到的步骤在Django后端上实现了CORS.本质上,我执行了以下步骤-
I implemented CORS on my Django backend by installing django-cors-headers
and following the steps mentioned in- https://github.com/OttoYiu/django-cors-headers
. Essentially, I performed the following steps-
-
pip install django-cors-headers
-
INSTALLED_APPS = ( ... 'corsheaders', ... )
-
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ]
pip install django-cors-headers
INSTALLED_APPS = ( ... 'corsheaders', ... )
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ]
CORS_ORIGIN_WHITELIST = (
'localhost:8000',
#LB
'10.254.138.226:443'
)
CORS_ORIGIN_WHITELIST = (
'localhost:8000',
#LB
'10.254.138.226:443'
)
使用带角度的前端时,我仍然无法使其正常工作.例如,我的POST请求变为OPTIONS.
I still can not make it work when I use my angular front-end. For example, my POST request becomes 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", …}
使用邮递员时,我没有这个问题.有人可以帮忙吗?
I don't have this problem when I use postman. Could someone help?
为了提供更多的上下文,我让前端和后端(DRF)运行在两台不同的计算机上.实际上,我有两个在两个不同的机器上运行的后端实例,并且我有一个负载平衡器,将请求从<LB IP>:443
映射到<Backend IP>:8000
基本URL之一.
To provide more context- I have the front end and the back end (DRF) running on two different machines. In fact, I have 2 instances of the back end running in two different machines and I have a load balancer mapping the requests from <LB IP>:443
to one of the <Backend IP>:8000
base-url.
推荐答案
我可能永远都不会找到有关发生原因的确切问题.我刚刚删除并重新创建了LB,一切开始自动运行.
I will possibly never find out the exact issue as to why it was happening. I just deleted and recreated the LB and everything started working automagically.
这篇关于CORS标头不适用于Django后端-Angular前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!