[英]How to make a POST request instead of GET when Ok is clicked in Confirmation Popup (Node, Express, Jade)
I have a web page rendering tables with rows and columns using Jade template.我有一个使用 Jade 模板的带有行和列的网页渲染表。 I need to make a POST request when i click "Ok" in confirmation box for delete.当我在确认框中单击“确定”进行删除时,我需要发出 POST 请求。
Now am able to create a Delete Confirmation popup with "Cancel" and "Ok" buttons.现在可以使用“取消”和“确定”按钮创建删除确认弹出窗口。
"Cancel" closes the confirmation box as expected. “取消”按预期关闭确认框。 But choosing "Ok" navigates and shows information(Actually a GET request).但是选择“确定”会导航并显示信息(实际上是 GET 请求)。 Instead of GET request, whenever "Ok" is clicked, it should make a POST request.每当点击“确定”时,它应该发出一个 POST 请求,而不是 GET 请求。
How can i make it a POST request?我怎样才能使它成为一个 POST 请求?
Code for Delete button,删除按钮的代码,
a.btn.btn-default(href='/removepage/'+value.PLANT+'/'+value.MATERIAL+'/'+value.CUSTOMER+'/'+value.RATE+'/'+value.CURRENCY+'/'+value.PRICE_UNIT+'/'+value.COND_UNIT+'/'+value.PORTAL_USER, onClick="return confirm('Are you sure to delete?')") Delete
Code for GET request, GET 请求的代码,
router.get('/removepage/:PLANT/:MATERIAL/:CUSTOMER/:RATE/:CURRENCY/:PRICE_UNIT/:COND_UNIT/:PORTAL_USER', (req, res) => {
console.log("Render check");
// console.log(req.params)
res.render('removepage',
{ title: 'Delete Page - Are you sure to delete?', plant: req.params.PLANT, material: req.params.MATERIAL,
currency: req.params.CURRENCY, rate: req.params.RATE, price_unit: req.params.PRICE_UNIT,
cond_unit: req.params.COND_UNIT, customer: req.params.CUSTOMER, portal_user: req.params.PORTAL_USER});
});
Code for POST request, POST 请求的代码,
router.post('/removepage', (req, res) => {
console.log("Hello", req.body);
client.connect(() => {
console.log('Connecting');
client.invoke("ZSD_CP_PRICE_DELETE",
{P_PLANT: req.body.P_PLANT,
P_MATERIAL: req.body.P_MATERIAL,
P_CUSTOMER: req.body.P_CUSTOMER
},
(err, result) => {
console.log('Invoking')
if (err) {
console.log(err)
return err;
}
console.log(result);
});
res.redirect('/');
});
});
Here is javascript for send POST request这是用于发送 POST 请求的 javascript
fetch('/removepage', {
method: 'POST'
}, {
P_PLANT: value.P_PLANT,
P_MATERIAL: value.P_MATERIAL,
P_CUSTOMER: value.P_CUSTOMER
})
You need to attach this to your "ok" button click您需要将此附加到您的“确定”按钮单击
a.btn.btn-default onClick="handlerOK(value.PLANT, value.MATERIAL, value.CUSTOMER)") Delete
Define handlerOk function at external file "script.js" and add reference to jade like this:script(src="/script.js")在外部文件“script.js”中定义 handlerOk 函数并像这样添加对 jade 的引用:script(src="/script.js")
function handlerOK(PLANT, MATERIAL, CUSTOMER) {
const confirmed = confirm('are you sure you want to delete?');
if (!confirmed) return;
fetch('/removepage', {
method: 'POST'
}, {
P_PLANT: PLANT,
P_MATERIAL: MATERIAL,
P_CUSTOMER: CUSTOMER
})
}
You could send a XMLHttpRequest :你可以发送一个XMLHttpRequest :
var xhr = new XMLHttpRequest();
xhr.open("POST", '/removepage', true);
var body = ""; // Anything you want to send
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = () => {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
//treating the result here
}
}
xhr.send(body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.