【问题标题】:Axios 404 (NOT FOUND) Error in React.js/Node.js Full-Stack ApplicationReact.js/Node.js 全栈应用程序中的 Axios 404 (NOT FOUND) 错误
【发布时间】: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


【解决方案1】:

尝试不将用户放入对象中

 confirm(event) {
    event.preventDefault();
    const 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).then(function (res) {
        alert("Thank you.  You are now registered.")
        window.location.href="/admin";
        // res.redirect(res, "/admin");
      })
      .catch(function (error) {
        alert(error.toJSON());
      });
      };

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多