我想尝试通过DOM从前端到后端检索数据,但我不知道该怎么做。 我使用POST来console.log数据,但是如何在Express后端中检索它? (console.log运行良好)
我的JS:
let urlSave = hitsItem.querySelector('.link-item').href;
let imgSave = hitsItem.querySelector('.infinitehits__img').src;
let marqueSave = hitsItem.querySelector('.hit__marque').textContent;
let smSave = hitsItem.querySelector('.hit__sous_modele2').textContent;
let typeSave = hitsItem.querySelector('.hit__type').textContent;
let yearsSave = hitsItem.querySelector('.hit__year_interval').textContent;
let coteEuSave = hitsItem.querySelector('.cote_actual_eu').textContent;
let coteUsdSave = hitsItem.querySelector('.cote_actual_usd').textContent;
let coteGbSave = hitsItem.querySelector('.cote_actual_gb').textContent;
let coteChfSave = hitsItem.querySelector('.cote_actual_chf').textContent;
let modelSave = {
urlSave: urlSave,
imgSave: imgSave,
marqueSave: marqueSave,
smSave: smSave,
typeSave: typeSave,
yearsSave: yearsSave,
coteEuSave: coteEuSave,
coteUsdSave: coteUsdSave,
coteGbSave: coteGbSave,
coteChfSave: coteChfSave
}
//console.log(modelSave)
// Get datas to save
$.post('/fr/save', () => {
console.log(modelSave);
})
我的节点后端:
router.post('/save', ensureAuthenticated, (req, res) => {
res.send('yep')
})
router.get('/save', ensureAuthenticated, (req, res) => {
res.send('yep')
})
答案 0 :(得分:1)
第1步。实际上将数据发送到后端:
$.post( '/fr/save', modelSave );
第2步。在后端执行一些操作:
router.post('/save', ensureAuthenticated, (req, res) => {
console.log(req.body)
res.send('yep')
})
确保已加载Express json bodyparser中间件:
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/json
app.use(bodyParser.json())