最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用获取发送表单数据

运维笔记admin10浏览0评论

使用获取发送表单数据

使用获取发送表单数据

我正在尝试使用提取发送表单数据。我有以下代码:

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));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论