django - Django - 进行 POST 时请求的资源上不存在“Access-Control-Allow-Origin”标头
问题描述
每当我在我的自定义身份验证类中发出 request.post 时,响应需要一段时间然后抛出这个错误 (502)
从源http://ec2-54-185-235-97.us-west-2.compute.amazonaws.com在“ http://54.185.235.97:8000/rest-auth/login/ ”访问 XMLHttpRequest ' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
这是代码(如果他/她超过登录尝试次数,此代码会向用户发送电子邮件以重置密码)
headers = {'Access-Control-Allow-Origin': '*'}
requests.post(
"http://ec2-54-185-235-97.us-west-2.compute.amazonaws.com:8000/rest-auth/password/reset/",
data={'email': user.email},
headers=headers
)
user.is_blocked = True
user.save()
attemptsObject.delete()
raise exceptions.AuthenticationFailed(('User Blocked'))
此代码在我的开发工作区中运行良好,但在我的 AWS 实例中出现了问题,这与我的 Nginx 配置有关吗?还是我需要创建自定义 CORS 中间件类?
Nginx:
upstream django_app {
server unix:/var/www/backend/run/gunicorn.sock fail_timeout=10s;
}
server {
listen 8000;
server_name localhost;
access_log /var/www/backend/logs/nginx-access.log;
error_log /var/www/backend/logs/nginx-error.log warn;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django_app;
}
location ~* \.(eot|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
}
解决方案
推荐阅读
- jquery - 如何自定义“require_from_group”错误消息并只显示一次?
- python - 当值的长度与索引的长度不匹配时,用 Nan 填充
- angular - 如何检测@Input 字符串的变化
- java - 保存在eclipse中的.java文件充满了空字符
- python - Sklearn Multilabel ML:ValueError:标签二值化不支持多输出目标数据
- elasticsearch - match_phrase_prefix 查询不适用于嵌套聚合
- xml - Xslt 1.0 如果来自节点的元素值为真,则从同一节点获取另一个元素的值
- python - 使用 python 和 matplotlib 绘制直方图
- user-interface - 在 Julia 中创建响应式绘图
- wix - Wix 在主要升级时保留防火墙规则,在卸载时删除