CORS header not working for Django backend- Angular frontend

穿精又带淫゛_ 提交于 2019-12-25 00:51:04

问题


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-

  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' )

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?

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.


回答1:


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.



来源:https://stackoverflow.com/questions/50846958/cors-header-not-working-for-django-backend-angular-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!