【问题标题】:Getting response object on 401 in AngularJS在 AngularJS 中获取 401 上的响应对象
【发布时间】:2017-08-05 14:11:23
【问题描述】:

我正在使用 Angular 1.6.4、Express 4.15.2 和 express-session。 我试图通过检查 req.session.user 参数的存在来判断用户是否未经授权访问某个路由。如果不是,我想发送 401 响应状态并更改 Angular 中的状态。

问题是我没有得到任何响应对象来检查其状态。 我尝试使用拦截器,注销 error.response.body,真正注销所有内容以找出我丢失响应对象的位置。

这里有一些代码,任何帮助将不胜感激!

快递:

app.get('/update', sessionCheck, function(req, res) {
  res.send('session');
});

function sessionCheck(req, res, next){
    if(req.session.user) {
      next();
    } else {
      console.log('before');
      return res.status(401).send('Unauthorized');
      console.log('after');
    }
}

角度:

.state('update', {
  url: '/update',
   views: {
    "": {
      templateUrl: 'templates/update.html',
      controller: function($http) {
        return $http.get('/update').then(function(response) {
          console.log('Ok response' + response);
        }, function(error) {
          console.log('Error response' + error.response.body);
        });
      },
    },
    "carousel": {
      templateUrl: "templates/carousel.html"
    },
    "footer": {
      templateUrl: "templates/footer.html"
    }
  }
})

network screen

【问题讨论】:

  • 您在“网络”标签请求中看到了什么?
  • @mehulmpt 添加了屏幕链接到帖子
  • 您得到了正确的响应。您在 nodejs 代码中为未经授权的用户发送 401 未经授权,因此它会以这种方式显示在您的网络选项卡中
  • @mehulmpt 好的,但是在控制器中我无法访问响应对象来设置像(response.status == 401)这样的条件。有什么办法吗?

标签: javascript angularjs express


【解决方案1】:

您是否尝试过使用拦截器来做到这一点?

你可以这样试试:

anyModule.service('yourInterceptor', function($q) {
var service = this;

service.responseError = function(response) {
    if (response.status == 401){
        //do something
    }
    return $q.reject(response);
};

})

请注意,这里我们正在处理 responseError

你也需要在配置函数中注册你的拦截器:

$httpProvider.interceptors.push('yourInterceptor');

你可以看到这个关于这个拦截器的更多信息:

Capture HTTP 401 with Angular.js interceptor

更新:

你也可以用这种方式注册一个拦截器:

app.factory("YourInterceptor", ["$q", "$rootScope", "$location",
function($q, $rootScope, $location) {
    var success = function(response) {
        //do something
        return response;
    },
    error = function(response) {
        if(response.status === 401) {
                // do something
        }

        return $q.reject(response); //reject on error
    };

    return function(httpPromise) {
        return httpPromise.then(success, error);
    };
}

然后你以这种方式注册你的拦截器(在模块的配置中):

$httpProvider.responseInterceptors.push("YourInterceptor");

请注意,您在 responseInterceptors 中推送拦截器。这对我有用。

【讨论】:

  • 我有。里面没有条件,只有console.log('string')。适用于其他路线,但在这条路线上没有任何显示。
  • 您可以尝试将 'responseError' 替换为 'response' 并查看拦截器中记录的内容。请注意,如果您使用“响应”执行此操作,则不能只拒绝所有承诺。
  • angular.module('app.providers') .service('authInterceptor', [ '$q', '$injector', '$location', function($q, $injector, $location) { return { request: function(config) { console.log('1'); return config; }, requestError: function(config) { console.log('2'); return config; }, response: function(res) { console.log('3'); return res; }, responseError: function(res) { console.log('4'); return res; } } }]) 在 '/update' 上什么也没给我。
  • 只是为了确定,您是否将此提供程序推送到 $httpProvider ?您必须在模块的配置函数中执行此操作。我用另一个解决方案编辑了响应。
猜你喜欢
  • 2014-05-31
  • 2015-02-12
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 2017-03-13
  • 2017-11-29
相关资源
最近更新 更多