node.js - 插入后无法重定向
问题描述
插入成功后,我想重定向到一个 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,如上所述。我希望它会帮助你。
推荐阅读
- python - 如何为绘图添加标签?
- nanomsg - nanomsg (nng) 中的多个发布者和订阅者
- python-3.x - 在python中导入自己的文件
- android - 如何在列表视图项目中添加图像按钮?
- gulp - 使用 gulp-zip 压缩主目录并排除子目录
- knockout.js - 单击不使用 KnockoutJS
- javascript - Spring MVC:需要优雅的解决方案来处理传递到前端的字符串的转义和取消转义
- python - 在上传的 zipfile 中包含 Lambda 的 Python 依赖项或从 S3 存储桶下载?
- wordpress - 第一次点击wordpress链接不起作用
- sql - SQL COUNT DISTINCT 输出不正确