angularjs http拦截器responseError对象

问题描述 投票:0回答:1

我通过在拦截器的responseerror对象中的rejection.headers()函数得到低于对象,但是无法在angularjs代码中获取特定的头部值,例如x-request-id值必须存储在一些变量中但是无法做到这一点可以任何一个请建议,

{pragma:“no-cache”,日期:“星期四,2019年9月6日14:57:56 GMT”,x-content-type-options:“nosniff”,x-request-id:“VLCRpt3v”,x-frame -options:“DENY”,...} cache-control:“no-cache,no-store,max-age = 0,must-revalidate”content-type:“application / json; charset = UTF-8”date:“星期四,06 Sep 2018 14:57:56 GMT“expires:”0“pragma:”no-cache“referrer-policy:”same-origin“transfer-encoding:”chunked“x-content-type-options:”nosniff “x-frame-options:”DENY“x-request-id:”VLCRpt3v“x-xss-protection:”1; mode = block“

并在angularjs代码中尝试下面的代码:var head = rejection.headers(); var requestId = head.x-request-id

angularjs interceptor
1个回答
0
投票

你最好创建一个Interceptor并将其推送到$httpProvider拦截器。

这是它应该是什么样子:

angular.module('app')
      .service('headerRetrieveInterceptor', function ($q) {
        var service = this;
        service.responseError = function (response) {
            // Here Are Your Headers
            console.log(response.headers());

            return $q.reject(response);
        };
    }).config(function($httpProvider) {
        $httpProvider.interceptors.push('headerRetrieveInterceptor');  
})
© www.soinside.com 2019 - 2024. All rights reserved.