在Javascript和Express中从前向后发送数据

问题描述 投票:0回答:1

我创建了一个变量来捕获来自前端的数据,并希望在Express后端中检索它。我的js变量和POST请求:

const dataPush = {
                urlSave: urlSave,
                imgSave: imgSave,
                marqueSave: marqueSave,
                smSave: smSave,
                typeSave: typeSave,
                precisionSave: precisionSave,
                yearsSave: yearsSave,
                coteEuSave: coteEuSave,
                coteUsdSave: coteUsdSave,
                coteGbSave: coteGbSave,
                coteChfSave: coteChfSave
            }
            let postIdUrl = String('/fr/post')
            fetch(postIdUrl, {
                method: "POST",
                body: JSON.stringify({"data": "dataPush"}),
                headers: { "Content-Type": "application/x-www-form-urlencoded" }
            })
            console.log(dataPush)

没关系,我有我需要的所有数据。

我的Express代码:

router.post('/post', (req, res) => {
    console.log('yep !')
});

如何从dataPush变量中检索数据?

javascript express fetch
1个回答
0
投票

发送时从""变量中删除dataPush标记:

        fetch(postIdUrl, {
            method: "POST",
            body: JSON.stringify({"data": dataPush}),
            headers: { "Content-Type": "application/x-www-form-urlencoded" }
        })

要获取变量,请使用:

router.post('/post', (req, res) => {
    console.log(req.data)
});
© www.soinside.com 2019 - 2024. All rights reserved.