有没有办法将帖子数据动态插入到在不同地址加载的 html 文件中?



我正在使用'node.js'和'express.js'。我想这样做: 1. 如果您发送带有"/管理/付款/更新"的 POST 请求,则后处理例程将获取查询结果语句。 2.我需要在"/"页面上显示这一点。"/"显示索引.html,它有一个表。

在这种情况下,如何将数据从 post 例程传递到索引.html文件?

router.post("/payment/update", (req, res) => {
const ProductName = req.body.ProductName;
const Price = req.body.Price;
database
.query("SELECT ProductName FROM screen")
.then(rows => {
console.log(rows);
return JSON.stringify(rows).includes(ProductName);
})
.then(state => {
state ? updateTable() : insertTable();
})
.catch(err => {
console.log(err);
res.send(err);
});
function updateTable() {
var updateStatement = "UPDATE screen SET ".concat(
"Amount = Amount + 1, TotalPrice = TotalPrice + ",
Price,
" WHERE ProductName = '",
ProductName,
"';"
);
database
.query(updateStatement)
.then(rows => {
return database.query(
"SELECT ProductName, Amount, TotalPrice FROM screen"
);
})
.then(rows => {
console.log(rows);
//this is where i wanna send data into index.html
})
.catch(err => {
console.log(err);
res.send(err);
});
}
function insertTable() {
var insertStatement = "INSERT INTO screen(ProductName, Amount, TotalPrice) VALUES(".concat(
"'",
ProductName,
"', ",
1,
",",
Price,
");"
);
database
.query(insertStatement)
.then(rows => {
return database.query(
"SELECT ProductName, Amount, TotalPrice FROM screen"
);
})
.then(rows => {
console.log(rows);
//this is also i wanna send data into index.html
})
.catch(err => {
console.log(err);
res.send(err);
});
}
});

您可以将redirect()与查询字符串一起使用,以将某些数据传递到其他页面。

例:

const url = require( 'url' );
res.redirect( url.format( {
pathname: '/',
query: {
foo: 'bar',
bar: 'foo'
}
} ) );

或者,如果您使用的是 View Engine,则可以使用res.render()。类似res.render(index.jade, data)

最新更新