我通过安装django-cors-headers并遵循github/OttoYiu/django-cors-headers中提到的步骤在Django后端上实现了CORS.本质上,我执行了以下步骤-
I implemented CORS on my Django backend by installing django-cors-headers and following the steps mentioned in- github/OttoYiu/django-cors-headers. Essentially, I performed the following steps-
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middlewaremon.CommonMiddleware', ... ]
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middlewaremon.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 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后端
发布评论