javascript - 在 Javascript 和 Express 中从前到后发送数据
问题描述
我创建了一个变量来从我的前端捕获数据,并希望在我的 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)
没关系,我得到了我需要的所有数据。
我的快递代码:
router.post('/post', (req, res) => {
console.log('yep !')
});
如何从dataPush
变量中检索数据?
解决方案
您的提取请求。删除正在传递的变量上的引号,并可能作为 JSON 传递
fetch(postIdUrl, {
method: "POST",
body: JSON.stringify({ "data": dataPush }),
headers: { "Content-Type": "application/json" }
})
您的端点
router.post('/post', (req, res) => {
console.log(req.body)
});
推荐阅读
- python - Python:使用 exec() 从其他文件导入列表
- python - 如何在每个循环中打印列表的不同元素?
- c# - 有什么方法可以“排列”一组图片框以在 for 语句中使用?
- kotlin - 房间关系在扩展时指定列名
- javascript - 查找出现奇数次的项目
- python - AttributeError:“元组”对象没有属性“编码”-MySQL.connector.Python
- python - 未能在定义的函数中为变量赋值
- java - 骆驼码头端点接受没有证书的客户端消息
- .net-core - 带有 MSAL 登录的 Microsoft Teams 选项卡
- nginx - 如何在地图中使用 nginx 正则表达式来提取 json 中键的值