0

I add errorHandlerInterceptor to handle errors as you seen below:

(function() {
    'use strict';

    angular
        .module('rasool')
        .config(httpConfig);

    function httpConfig($httpProvider) {

        $httpProvider.interceptors.push('errorHandlerInterceptor');
    }
})();

and in the errorHandlerInterceptor i want to get a specific header from response headers like this:

(function() {
    'use strict';

    angular
        .module('rasool')
        .factory('errorHandlerInterceptor', errorHandlerInterceptor);

    function errorHandlerInterceptor ($q, toaster, $rootScope, $timeout) {
        var service = {
            responseError: responseError
        };

        return service;

        function responseError (response) {

            var errorHeader = response.headers('X-app-alert');

            if(errorHeader) {
                // do something
            }

            return $q.reject(response);
        }
    }
})();

The problem is that the errorHeader is null, but when i check the response headers with Chrome DevTools, this header exists and have a value as you can see in the below screenshot:

enter image description here

UPDATE

All of these part are running correctly in the website, but i created a copy of html, js and css files to create an ionic1 application, but when i run ionic serve command to test the application, i have this problem.

Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
  • Are you sure that by the time you are trying to access the response header the promise is already resolved? – Setup May 26 '17 at 11:29
  • The non-standard header needs to be listed with [Access-Control-Expose-Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers). – georgeawg May 26 '17 at 11:58
  • @georgeawg, as you seen above in the screenshot, `X-app-alert` already exists in the response headers, but angular does not get this header – Rasool Ghafari Jun 01 '17 at 12:14

1 Answers1

0

Did you try to call it like this:

var errorHeader = response.headers();

and after errorHeader should be an object that have the data you need.

Tonio
  • 4,082
  • 4
  • 35
  • 60