[英]Send datas from front to back in Javascript and Express
I created a variable to catch data from my frontend and want to retrieve it in my Express backend.我创建了一个变量来从我的前端捕获数据,并希望在我的 Express 后端检索它。
My js variable and POST request:我的 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)
And it's okay, I get all the data I need.没关系,我得到了我需要的所有数据。
My Express code:我的快递代码:
router.post('/post', (req, res) => {
console.log('yep !')
});
How to retrieve data from the dataPush
variable?如何从
dataPush
变量中检索数据?
Your fetch request.您的提取请求。 Remove quotation marks on variable being passed and perhaps pass as JSON
删除正在传递的变量上的引号,并可能传递为 JSON
fetch(postIdUrl, {
method: "POST",
body: JSON.stringify({ "data": dataPush }),
headers: { "Content-Type": "application/json" }
})
Your endpoint您的端点
router.post('/post', (req, res) => {
console.log(req.body)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.