ajax成功后,Node Express路由到新窗口位置

时间:2017-02-11 04:20:58

标签: javascript node.js

在通过初始网址/访问我的网页时,我有一个输入,其中需要密码才能查看下一页。我不太确定如何在成功密码匹配时将用户重定向到下一页。

客户来电:

var urlToCall = baseUrl + '/info?pass=' + password;
$.ajax({
  'url' : urlToCall,
  'type' : 'GET'
})
.done(function(data){
  console.log(data)
})
.fail(function(error){
  console.log(error);
});

节点/快递:

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。现在,如果我在浏览器中转到url而不是通过密码https://myUrl.com/info?pass=mypassword,它会成功将我路由到它。所以通过ajax检查,我该怎么做呢?

res.render只给我回复html,但是我需要将用户告诉我这个页面。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

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);
});