我创建了一个变量来捕获来自前端的数据,并希望在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
变量中检索数据?
发送时从""
变量中删除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)
});