AngularJS 1.4

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdn.rawgit.com/angular-ui/bootstrap-bower/0.13.4/ui-bootstrap-tpls.js"></script>
<body ng-app="myApp" ng-controller="testCtrl">

</body>

JavaScript

'use strict';

  // Declare app level module which depends on views, and components
  var app = angular.module('myApp', []);




  app.factory('authHttpResponseInterceptor', ['$q', '$location', function($q, $location) {
      return {
      		request: function(config) {
          	return config;
          },
          requestError: function(err) {
          	$q.reject(err);
          },
          response: function(response) {
              console.log('response');
              if (response.status === 401) { // It will never be fired for 401 because angular always redirects 401 to responseError method
                  console.log("Response 401"); 
              } else {
                  console.log("other response but no 401");
              }
              return response || $q.when(response);
          },
          responseError: function(rejection) { // It will be fired if 401 occurs
              if (rejection.status === 401) { 
                  console.log("Response Error 401", rejection);
              }
              return $q.reject(rejection);
          }
      }
  }]);

  app.config(['$httpProvider', function($httpProvider) {
      //Http Intercpetor to check auth failures for xhr requests
      $httpProvider.interceptors.push('authHttpResponseInterceptor');
  }]);

  app.controller('testCtrl', function($scope, $http, $timeout) {
      $http.get('http://httpstat.us/401')
      .success(function(data) { // Use .then instead of .success (success is deprecated!)
          alert(data.id);
      });

  });