简体   繁体   English

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

[英]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.

相关问题 从 React Native 前端发送数据到 Express 后端 - Send data from React Native front end to Express back end 如何使用javascript或angularjs将文件从前端发送到后端 - how to send a file from front end to back end with javascript or angularjs 如何将数据从python(烧瓶)发送到javascript? - How to send datas from python (flask) to javascript? 如何将图像尺寸从 javascript 发送到后端(快递) - How to send image dimension from javascript to back-end (express) 将数据从 javascript 前端发送到 Flask 后端而不使用 Ajax 的方式 - Way to send data from javascript front-end to Flask back-end WITHOUT Ajax Javascript - 将用户输入从前端发送到后端服务器 - Javascript - Send User Input from front-end to back-end server 如何使用 WebSocket 从前端 Javascript 发送图像到后端 Flask? - How to send and image from Javascript front-end to Flask back-end with WebSocket? Javascript,从前到后移动数组中的元素 - Javascript, moving elements in an array from front to back 在Node / Express中将用户输入从前端传递到后端 - Pass user input from front end to back end in Node/Express 如何从前端JavaScript发送JSON数据到使用Python搭建的后端Server Flask - How to send JSON data from front-end JavaScript to the Back-end Server built by using Python Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM