【发布时间】:2020-11-03 15:42:15
【问题描述】:
我正在构建一个全栈 MERN 应用程序。我在前端有一个表单,我想将输入值传递到后端以向 SQL Server 发布帖子,但我收到 404(未找到)错误。如果您发现任何可能导致错误的内容,请告诉我。提前谢谢你!
前端后调用到后端
confirm(event) {
event.preventDefault();
var User = {
firstName: this.state.firstName,
lastName: this.state.lastName,
email: this.state.email,
password: this.state.password
};
instance
.post("http://localhost:3001/api/signup", {
User: User
})
.then(function (res) {
alert("Thank you. You are now registered.");
window.location.href = "/admin";
// res.redirect(res, "/admin");
})
.catch(function (error) {
alert(error.toJSON());
});
}
后端
app.post("/api/signup", cors(), function (req, res) {
console.log(req.body);
sql
.connect(config)
.then(() => {
return sql.query`INSERT INTO fantasy_scrape.dbo.clientele (user_first_name, user_last_name, email, password, join_date) VALUES ('*******', '*******', '****.*******@gmail.com', '*********', GETDATE())`;
})
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
});
});
【问题讨论】:
-
你能在后端发布你的完整应用代码吗?前端看起来不错。
标签: node.js reactjs express axios