通过初始网址/
访问我的网页后,我有一个输入,需要输入密码才能查看下一页。我不太确定如何在密码匹配成功后将用户重定向到下一页。
客户电话:
var urlToCall = baseUrl + '/info?pass=' + password;
$.ajax({
'url' : urlToCall,
'type' : 'GET'
})
.done(function(data){
console.log(data)
})
.fail(function(error){
console.log(error);
});
Node/ express :
var info = require('./routes/info');
app.use('/info', info);
router.get('/', function(req, res, next) {
//... logic to check password and such
res.render('info', { title: 'Book Info' });
})
问题是,如果成功,我会从文件 info.ejs
返回一个 html 字符串。现在,如果我在浏览器中访问该网址,而不是通过密码 https://myUrl.com/info?pass=mypassword
,它会成功将我路由到该网址。那么通过ajax检查,我该如何做呢?
res.render
只给我返回 html,但我需要将用户路由到此页面。
感谢您的帮助
最佳答案
var info = require('./routes/info');
app.use('/info', info);
router.get('/', function(req, res, next) {
res.render('input-passwordpage.html');
})'
router.post('/', function(req, res, next) {
//... logic to check password and such
if(req.body.password === "password")
res.json{('info', { title: 'Book Info' }});
else
res.json({});
})'
var urlToCall = baseUrl + '/info?pass=' + password;
$.ajax({
'url' : urlToCall,
'type' : 'POST'
})
.done(function(data){
var data = JSON.parse(JSON.stringify(data));
console.log(data);
//according to returned data structure you can access its properties.
if(data.info) {
$.html('you html here ');
//you can also use $.load depending upon your requirements
/// also window.location.href will also work depending upon your logic and requirement
}
})
.fail(function(error){
console.log(error);
});
关于javascript - Ajax 成功后 Node Express 路由到新窗口位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42172196/