简体   繁体   English

Angular拦截器中的响应标头

[英]Response headers in Angular interceptor

I have an interceptor for authentication. 我有一个用于身份验证的拦截器。

I want to get a header out of the response when I get a 401 response error. 当我得到401响应错误时,我想从响应中获取标题。

Interceptor is: 拦截器是:

function ($httpProvider, fileUploadProvider) {

    $httpProvider.interceptors.push(function($q, $localStorage) {
  return {
   'request': function(config) {
       if ($localStorage.token) {
           config.headers.Authorization = 'Bearer ' + $localStorage.token;
       }

       return config;
    },

    'responseError': function(response) {
       if (response.status === 401) {
          //$rootScope.$broadcast('unauthorized');  

          // WWW-Authenticate: Bearer error="invalid_token"

          var authResult = response.headers('WWW-Authenticate');
          if (authResult.indexOf("invalid_token")>-1) {
              $localStorage.token = null;
              $timeout(function(){
                ;
              });
          }         
       }

       return response;
    }
  };

I want to get the WWW-Authenticate header from the response. 我想从响应中获取WWW-Authenticate标头。

I can confirm the header is in the response of the web service call by looking at the network tab in Chrome developers tools. 通过查看Chrome开发人员工具中的网络标签,我可以确认标题位于Web服务调用的响应中。 If I set a break point in the response handler function and then run console.log(response.headers()) in the console I get: 如果我在响应处理程序函数中设置了一个断点,然后在控制台中运行console.log(response.headers()),我得到:

Object {}
undefined

How do I get to the response headers? 如何进入响应标头?

The responseError function receives rejection instead of response . responseError函数接收rejection而不是response Therefore if you want to access response headers, what you need is like below. 因此,如果您想访问响应标头,您需要的是如下所示。

'responseError': function(rejection) {
    if (rejection.status === 401) {
        console.log(rejection.config.headers);
    }
}

I hope this would help you. 我希望这会对你有所帮助。 :) :)

Although I know this is not answer and should post as comment, I post it here to use screen capture image. 虽然我知道这不是答案,应该发表评论,但我在这里发布使用屏幕截图。

I tried to get a response header with my test enviroment like below. 我尝试使用下面的测试环境获得响应标头。

nodejs server nodejs服务器

res.setHeader('WWW-Authenticate', 'invalid_token');
res.status(401).send();

angularjs angularjs

'responseError': function(rejection) {
    if (rejection.status === 401) {
        console.log(rejection.headers('WWW-Authenticate'));
    }
}

Chrome dev tool screen capture Chrome开发工具屏幕截图

在此输入图像描述

As you can see, I could get the response header correctly. 如您所见,我可以正确获取响应头。 Therefore I think that there seems to be some problem in your server code where you set a response header. 因此,我认为在您设置响应标头的服务器代码中似乎存在一些问题。

Would you like to show us your chrome dev tool screen capture and your server code where you set the response header? 您是否要向我们展示您的chrome dev工具屏幕捕获以及设置响应标头的服务器代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM