Node.js 如何将React表单数据发送到后端express服务器
我制作了这个react表单,希望将候选对象发送到后端express服务器,在那里我希望控制台记录候选对象。我已经检查了表单是否正确输入了内容。我正在使用axios向express后端发送post请求Node.js 如何将React表单数据发送到后端express服务器,node.js,express,axios,Node.js,Express,Axios,我制作了这个react表单,希望将候选对象发送到后端express服务器,在那里我希望控制台记录候选对象。我已经检查了表单是否正确输入了内容。我正在使用axios向express后端发送post请求 import React, { Fragment, useState } from "react"; import axios from "axios"; const Form = () => { const [candidate, setCand
import React, { Fragment, useState } from "react";
import axios from "axios";
const Form = () => {
const [candidate, setCandidate] = useState({
fullName: "",
phoneNumber: 0,
email: "",
gitProfile: "",
linkToResume: "",
designation: "",
interest: "",
});
const onChange = e =>
setCandidate({ ...candidate, [e.target.name]: e.target.value });
const onSubmit = e => {
e.preventDefault();
axios
.post("http://localhost:5000/", {
candidate,
})
.then(res => {
console.log(res, candidate);
});
};
const designationOptions = [
"--select option--",
"Graphic Designer",
"Full Stack Developer",
"Project Manager",
"SEO and Digital Marketing",
];
return (
//form input code
);
};
export default Form;
这是后端express服务器代码
const express = require("express"),
bodyParser = require("body-parser");
(app = express()), (port = process.env.PORT || 5000), (cors = require("cors"));
app.use(
cors({
origin: "http://localhost:3000",
credentials: true,
})
);
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.get("/", function (req, res) {
console.log(req.body);
});
app.listen(port, () => console.log(`Backend server live on ${port}`));
我想发送候选对象和console.log对象,但收到404错误。
我将此设置放在父目录下的两个不同文件夹中。您使用的是
app.get
,同时使用axios作为POST
发送请求。
尝试切换
app.get
到app.post
这解决了我的问题。非常感谢你。我想将电子邮件凭据传递到同一目录中的另一个文件,以便进一步处理。你能帮我吗?