FastAPI Cookie Not Received by React Frontend: Resolution
When using FastAPI as the backend and React as the frontend communicated via AJAX requests, cookies set by FastAPI might fail to be received by React. This inconsistent behavior can be attributed to the distinct port numbers used by both applications, which creates a cross-origin request scenario.
To address this issue, several steps are necessary:
在您的React应用程序中配置AXIOS请求,以通过将带有Credentials属性设置为TRUE来接收Cookie。这对于交叉原始请求是必要的,要求包括cookie等凭据。 }); 在fastapi中指定允许的启动:由于请求的交叉孔子性质,因此您必须明确指定允许访问API的起源。这包括您的React前端的域和端口。
在corsmiddleware中允许凭证:
通过遵循以下步骤,FastApi应该成功地将cookie返回您的React FrontEnd。请记住要彻底检查cookie是否正确设置了,没有在Axios Post请求中返回错误。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3