Node.js 使用fetch发送表单数据
我正在尝试使用fetch发送表单数据。我有以下代码:Node.js 使用fetch发送表单数据,node.js,express,fetch,fetch-api,Node.js,Express,Fetch,Fetch Api,我正在尝试使用fetch发送表单数据。我有以下代码: 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 =>
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));
app.post("/sendform", function(req, res) {
console.log("processing form data");
console.log(req.body);
res.status(200).send("received");
});
然后,我尝试使用以下代码在Express后端读取表单数据:
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));
app.post("/sendform", function(req, res) {
console.log("processing form data");
console.log(req.body);
res.status(200).send("received");
});
我有身体分析器等等。
但是,req.body是一个空对象。
有人能帮忙修理吗?试试这种类型
fetch(config.DOMAIN + "user/addPost", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify(postData)
})
.then(res => res.json())
.then(post => {
console.log(post)
})
.catch(err => {
console.log(err)
});