来源检查失败 - http://localhost:8000/ 与任何可信来源不匹配

问题描述 投票:0回答:1

我一直在努力解决从 React 开发服务器到我的 django 后端的登录表单 POST 请求的 CORS 问题,我在其中使用 Django 的 LOginView 模块进行登录请求 API。 这些是我的 settings.py 设置

CORS_ALLOW_METHODS = (
    "DELETE",
    "GET",
    "OPTIONS",
    "PATCH",
    "POST",
    "PUT",
)
CORS_ALLOW_HEADERS = (
    "accept",
    "authorization",
    "content-type",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
)
CORS_ALLOWED_ORIGINS =  [
     'http://localhost:3000',  
]


CORS_TRUSTED_ORIGINS = [
     'http://localhost:3000',  
]
CSRF_TRUSTED_ORIGINS = [
     'http://localhost:3000', 
]
CORS_ORIGINS_WHITELIST = [
     'http://localhost:3000',  
]

为了做出反应,我在 package.json 中实现了一个代理

"proxy": "http://localhost:8000/",

我的用户令牌检索 api 工作正常,但我相信 CSRF 验证有问题。

我已经尝试过文档中提到的设置: https://pypi.org/project/django-cors-headers/ 以及论坛中遇到类似问题的人 https://forum.djangoproject.com/t/origin-checking-failed-with-ssl-https/20158/13

reactjs django cors http-proxy
1个回答
0
投票

您收到的错误是针对

http://localhost:8000/
的,但您正在将
http://localhost:3000
添加到设置中,只需将端口更改为
8000
就应该没问题了

© www.soinside.com 2019 - 2024. All rights reserved.