我正在开发一个使用React前端和Node.js后端的web应用程序。前端使用这样的Axios向后端发送一个POST请求:
Register.js
...
handleSubmit = (e) => {
e.preventDefault();
const { email, password, name, dateofbirth } = this.state;
const user = { email, password, name, dateofbirth };
const url = "http://localhost:9000/register";
axios
.post(url, user, {
headers: {
"Content-Type": "application/json",
},
})
.then((response) => console.log(response))
.catch((error) => {
console.error("You have made a big error. " + error);
console.log(user);
});
};
...
当后端像这样接收请求时:
./routes/register.js
...
router.post("/register", async (req, res) => {
console.log("Inside Home Login");
res.writeHead(200, {
"Content-Type": "application/json",
});
console.log("Users : ", JSON.stringify(users));
res.end(JSON.stringify(users));
})
...
但是,在试图发送任何内容时,我会得到"POST http://localhost:9000/register 404 (未找到)“错误。
发布于 2020-10-01 05:59:42
我猜你是在index.js中路由。如果您可以提供一个代码示例来计算它。
如果是这样的话,事情就是定义一个路由,
app.use('/register', yourImportedVariable);
在http://localhost:9000/register上确实定义了一条路由。
因此,如果在您的路由/Regier.js文件中定义了一个GET端点并使用‘/注册’,则您的前端调用必须是http://localhost:9000/register/register。
要修复它,要么将您的路由重命名为'/',要么用上面的url修复您的前端调用。
https://stackoverflow.com/questions/64156396
复制相似问题