问题
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', ... ]
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