Reputation: 19297
I want to redirect to a url after the insert is success :
$(document).ready(function() {
$("#btn").on("click", function() {
$.post({
url:"/track/admin/affecterMenusProfil",
data:$("#frm").serialize()
});
});
});
router.post("/affecterMenusProfil", function(req, res) {
var profil = req.body.profil, menus = req.body.menus;
connexion.query("delete from "+db.getPrefixNomTables()+"profil_menu where profil_id ="+profil, function (errDel, rowsDel) {
if (errDel)
throw errDel;
async.eachOf(menus, function(menu, position, cb) {
connexion.query("insert into "+db.getPrefixNomTables()+"profil_menu(menu_id, profil_id) values("+menu+", "+profil+")", function (err, rows) {
if (err) throw err;
cb();
});
}, function() {
res.redirect('/track/');
});
});
});
But at runtime there is no redirection : the page of insertion is still displayed ! So what is wrong in my codes ?
Upvotes: 0
Views: 22
Reputation: 2353
Server won't redirect if your request from ajax.
In order to redirect you have to implement redirection logic on client side on success callback.
$(document).ready(function() {
$("#btn").on("click", function() {
$.post({
url:"/track/admin/affecterMenusProfil",
data:$("#frm").serialize(),
success: function(response) {
window.location.href = response.url;
}
});
});
});
router.post("/affecterMenusProfil", function(req, res) {
var profil = req.body.profil, menus = req.body.menus;
connexion.query("delete from "+db.getPrefixNomTables()+"profil_menu where profil_id ="+profil, function (errDel, rowsDel) {
if (errDel)
throw errDel;
async.eachOf(menus, function(menu, position, cb) {
connexion.query("insert into "+db.getPrefixNomTables()+"profil_menu(menu_id, profil_id) values("+menu+", "+profil+")", function (err, rows) {
if (err) throw err;
cb();
});
}, function() {
res.send({url: '/track/'});
});
});
});
You have to send URL from Server and redirect that URL from the client side as above. I hope it will help you.
Upvotes: 1