Node.js页面重定向AJAX登录?重定向后调用函数? [英] Node.js Page Redirect on AJAX login? With a function call after redirect?

查看:127
本文介绍了Node.js页面重定向AJAX登录?重定向后调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'/ stories'页面是特定于用户的,需要有效的登录凭据。

The '/stories' page is user-specific and requires valid login credentials.

验证是在节点,服务器端和正确日志之前进行的。重定向,但不会重定向到故事页面..

Validation is accounted for with node, server-side, and properly logs before the redirect, but does not redirect to the stories page..

这似乎是因为你不能在AJAX之后进行重定向。你需要在Javascript中自己做,但是这个问题的第一个答案中的代码似乎有点不完整......成功通话..

This seems to be because "You can't make a redirection after an AJAX. You need to do it yourself in Javascript", but the code in the first answer to this question seems somewhat incomplete..for the succcess call..

此外,如何在重定向后成功调用后续函数(refreshStories)?

Also, how can a subsequent function (refreshStories) be called on success after the redirect?

这是AJAX:

if (loginUsername.length != 0) {
  console.log('there is a login: ' + loginUsername);
    // make an ajax call
    $.ajax({
    dataType: 'json',
    data: AjaxLoginData,
    type: 'post',
      url:"http://localhost:4200/api/v1/users",
      success: (data, textStatus, jqXHR) ->
          if typeof data.redirect == 'string'
              window.location = data.redirect
      success: refreshStories,
      error: foundError
    });

Node.js + Express4

Node.js + Express4

router.route('/users')

    // log in a user (accessed at POST http://localhost:4200/api/v1/users)
    .post(function(req, res) {

      var username = req.body.loginUsername;
      var password = req.body.loginPassword;

      authenticateUser(username, password, function(err, user){
      console.log('authenticate user..');
        if (user) {
          console.log('yes user');
          // subsequent requests will know the user is logged in
          req.session.username = user.username;
          console.log('set session username to: ' + req.session.username);

          // res.redirect('/stories');
                res.send({redirect: '/stories'});
          console.log('user logged in .. redirect to stories');

        } else {
          console.log('user authentication badCredentials error..');
          res.render('index', {badCredentials: true});
        }
      });
    });


推荐答案

试试这个

if (loginUsername.length != 0) {
  console.log('there is a login: ' + loginUsername);
    // make an ajax call
    $.ajax({
    dataType: 'json',
    data: AjaxLoginData,
    type: 'post',
      url:"http://localhost:4200/api/v1/users",
      success: (data, textStatus, jqXHR) ->
          if (typeof data.redirect == 'string')
              window.location.replace(window.location.protocol + "//" + window.location.host + data.redirect);
      error: foundError
    });




window.location.replace(...)将最好地模拟一个HTTP重定向
如何重定向到JavaScript / jQuery中的另一个网页?

对于'refreshStories'的东西,你应该只是刷新故事转到'/ stories'

For your 'refreshStories' stuff, you should just refresh Stories when you go to '/stories'

这篇关于Node.js页面重定向AJAX登录?重定向后调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆