【发布时间】:2021-04-19 14:24:33
【问题描述】:
我一直在开发一个 React 应用程序来通过 express 后端对用户进行身份验证。我已经通过使用邮递员发送 POST 请求来测试快递服务器,一切正常。
但我在使用 React 应用程序执行相同的 POST 请求时遇到了一个奇怪的错误。
我检查了请求负载,发送的 JSON 数据是正确的。
下面给出的是 axios 操作的代码 sn-p。
export const registerUser=(userData, history) => dispatch => {
axios.post("http://localhost:5000/users/register",userData)
.then( res => {
console.log(res)
history.push("/login") } )
}
非常感谢任何有关如何解决此问题的指示。
【问题讨论】:
-
谢谢,是否有可能得到一个封装的答案?我的意思是,我应该添加必要的代码?
-
如果您在与服务器不同的端口上提供 React 应用程序,请参考此页面,我感觉这可能是代理问题 create-react-app.dev/docs/proxying-api-requests-in-development