首页 > 解决方案 > React.js, Node.js : 在 React 中发送 axios 请求

问题描述

我尝试使用 axios 在 react 中发送带有电子邮件和密码的请求。 React 应用程序:

      console.log(`
    --SUBMITTING--
    First Name: ${this.state.firstName}
    Last Name: ${this.state.lastName}
    Email: ${this.state.email}
    Password: ${this.state.password}
  `);
  this.sendData(this.state.email, this.state.password);
  // this.changeRoot("calendar");
} else {
  console.error("FORM INVALID - DISPLAY ERROR MESSAGE");
}

};

  sendData(login, password) {
console.log("login -> " + login);
console.log("passwor -> " + password);
axios.post('http://localhost:9000/signup', { email: login, psswd: password })
.then((rep) => { console.log(rep) })
.catch((error) => { console.log(error) });

}

登录名和密码中的数据都可以。节点服务器:

app.post('/signup', (req, res, next) => {
console.log("login -> " + req.body.email);
console.log("psswd -> " + req.body.psswd);

});

所以感谢所有的帮助。

标签: node.jsreactjsaxios

解决方案


只是猜测,但是,您似乎没有配置 bodyParser() 中间件(将数据从 HTTP 请求解析到req.body.

请参阅此相关答案


推荐阅读