Node.js 插入后无法重定向

Node.js 插入后无法重定向,node.js,express,Node.js,Express,插入成功后,我想重定向到url: $(document).ready(function() { $("#btn").on("click", function() { $.post({ url:"/track/admin/affecterMenusProfil", data:$("#frm").serialize() }); }); }); ro

插入成功后,我想重定向到url:

$(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/');
        });
    });
});

但是在运行时没有重定向:插入页面仍然显示!那么我的代码有什么问题吗?

如果您的请求来自ajax,服务器将不会重定向

为了重定向,您必须在客户端成功实现重定向逻辑 回拨

$(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/'});
        });
    });
});
您必须从服务器发送URL,并如上所述从客户端重定向该URL。 我希望它能帮助你