[英]View is not change after render .pug file from express
I have some problem that the html view is not change after render the pug file,after getting the server response. 我有一些问题,在得到服务器响应后,渲染pug文件后html视图不会改变。
The code is following 代码如下
app.set('view engine', 'pug');
app.set("views", require('path').join(__dirname, "views"));
app.post('/login', function(req, res, next) {
console.log(req.body);
checkExistanceOfuser(req.body.username, req.body.password, function(err, flag, data) {
console.log(err);
if (err) {
res.send({
err: 'SOME_DATABASE_ERROR'
})
} else {
if (flag) {
req.session.user = data;
/*res.send({
success: true,
message: 'Login Success'
})*/
res.render('welcome', { name: data.name });
} else {
/*res.send({
success: false,
message: 'Invalid Credentials'
});*/
res.render('login', { error: 'Invalid Credentials' });
}
}
})
But i check in network section from browser.The API response (preview) is fine. 但是我从浏览器中检查网络部分.API响应(预览)很好。
You're rendering views/login
, but you've already specified that the view folder is views
. 您正在渲染views/login
,但是已经指定view文件夹为views
。 Just render login
. 只需渲染login
。
when you are calling /login
route it's a post call and probably you are using ajax post call for doing so. 当您调用/login
route时,这是一个后期调用,可能您使用的是ajax后期调用。 Now when you are calling the /login
route its rendering the pug
file but its not actually effecting the browser DOM. 现在,当您调用/login
路由时,它将呈现pug
文件,但实际上并没有影响浏览器DOM。 So what you need to do is this 所以你需要做的是
create a new route like this 创建这样的新路线
app.get('/login-success', function(req, res, next) {
if (req.session && req.session.user) {
res.render('welcome', { name: req.session.user.name });
} else {
res.redirect('/');
}
});
and modify the login function like this 并像这样修改登录功能
app.post('/login', function(req, res, next) {
console.log(req.body);
checkExistanceOfuser(req.body.username, req.body.password, function(err, flag, data) {
console.log(err);
if (err) {
res.send({
err: 'SOME_DATABASE_ERROR'
})
} else {
if (flag) {
req.session.user = data;
res.send({
success: true,
message: 'Login Success'
});
} else {
res.send({
success: false,
message: 'Invalid Credentials'
});
}
}
})
});
in ajax call use this 在ajax调用中使用此
function login(){
$.post("http://localhost:8000/login",
{
username:document.getElementById("name").value,
password:document.getElementById("password").value,
},
function(data, status){
if(data.success){
window.location = "http://localhost:8000/login-success"
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.