使用获取发送表单数据
我正在尝试使用提取发送表单数据。我有以下代码:
var toSend = new FormData();
toSend.append('person', 'Peter');
toSend.append('age', '22');
toSend.append('thistext', 'smart');
fetch('http://localhost:3000/sendform', {
method: 'POST',
body: toSend
}).then(res => console.log(res));
然后我尝试使用以下代码在Express的后端读取表单数据:
app.post("/sendform", function(req, res) {
console.log("processing form data");
console.log(req.body);
res.status(200).send("received");
});
我有人体分析仪等。但是,req.body是一个空对象。有人可以帮忙解决吗?
回答如下:尝试此代码,
let payload = {
person: "peter",
age: "22",
thistext: "smart"
};
fetch("http://localhost:3000/sendform", {
method: "POST",
body: JSON.stringify(payload)
}).then(res => console.log(res));
使用获取发送表单数据
我正在尝试使用提取发送表单数据。我有以下代码:
var toSend = new FormData();
toSend.append('person', 'Peter');
toSend.append('age', '22');
toSend.append('thistext', 'smart');
fetch('http://localhost:3000/sendform', {
method: 'POST',
body: toSend
}).then(res => console.log(res));
然后我尝试使用以下代码在Express的后端读取表单数据:
app.post("/sendform", function(req, res) {
console.log("processing form data");
console.log(req.body);
res.status(200).send("received");
});
我有人体分析仪等。但是,req.body是一个空对象。有人可以帮忙解决吗?
回答如下:尝试此代码,
let payload = {
person: "peter",
age: "22",
thistext: "smart"
};
fetch("http://localhost:3000/sendform", {
method: "POST",
body: JSON.stringify(payload)
}).then(res => console.log(res));