使用 fetch 发送表单数据

Sending form data using fetch

我正在尝试使用 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 => console.log(res));

然后我尝试使用以下代码在后端使用 Express 读取表单数据:

app.post("/sendform", function(req, res) {
    console.log("processing form data");
    console.log(req.body);
    res.status(200).send("received");
});

我有 body-parser 等等。 但是,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)
        });