Tridip Bhattacharjee

Tridip Bhattacharjee

  • NA
  • 1.2k
  • 77.1k

What $httpProvider.interceptors.push does

Apr 11 2018 4:28 AM
i am new in angular when reading this article https://www.c-sharpcorner.com/UploadFile/ff2f08/token-based-authentication-using-Asp-Net-web-api-in-angularj/
 
i found few line of code which is not clear to me. those lines are
 
  1. .config(['$httpProvider', function ($httpProvider) {    
  2.     
  3.         $httpProvider.interceptors.push(function ($q, $rootScope, $window, $location) {    
  4.     
  5.             return {    
  6.                 request: function (config) {    
  7.     
  8.                     return config;    
  9.                 },    
  10.                 requestError: function (rejection) {    
  11.     
  12.                     return $q.reject(rejection);    
  13.                 },    
  14.                 response: function (response) {    
  15.                     if (response.status == "401") {    
  16.                         $location.path('/login');    
  17.                     }    
  18.                     //the same response/modified/or a new one need to be returned.    
  19.                     return response;    
  20.                 },    
  21.                 responseError: function (rejection) {    
  22.     
  23.                     if (rejection.status == "401") {    
  24.                         $location.path('/login');    
  25.                     }    
  26.                     return $q.reject(rejection);    
  27.                 }    
  28.             };    
  29.         });   

 please some one help me to understand the above few lines of code and their objective.
 

Answers (2)