【发布时间】:2021-01-17 05:40:27
【问题描述】:
我正在使用 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 (Not Found)”。
【问题讨论】:
-
你能通过邮递员或终端测试你的后端端点吗?也许你通过路由器不知何故不正确
-
你确定你的后端服务器正在运行吗,(我知道这听起来微不足道,但只是想想我的常见错误)
-
是的,API 肯定是开启的。
-
你可以在你添加路由器到应用程序的地方添加你的代码吗?
标签: javascript node.js reactjs http axios