CORS标头不适用于Django后端

编程入门 行业动态 更新时间:2024-10-24 10:27:06
本文介绍了CORS标头不适用于Django后端-Angular前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过安装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-

  • pip install django-cors-headers
  • INSTALLED_APPS = ( ... 'corsheaders', ... )
  • MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middlewaremon.CommonMiddleware', ... ]

  • pip install django-cors-headers
  • INSTALLED_APPS = ( ... 'corsheaders', ... )
  • 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后端

    本文发布于:2023-11-15 02:59:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1590542.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:后端   不适用于   CORS   Django

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!