DoctorV DoctorV - 2 years ago 195
AngularJS Question

AngularJS Interceptor to Redirect

ExpressJS is sending the following response...


I'm trying to get AngularJS to see this response via an Interceptor and perform a redirect. Does anyone have sample code where Angular catches a server response (such as my "ItemUploaded") and performs a redirect to a partial (via $location)?

Answer Source

Her is the factory for the interceptor.

 .factory('InterceptorService',['$q', '$location', function( $q, $location, $http){
     var InterceptorServiceFactory = {};

     var _request = function(config){
         //success logic her 
         return config;

     var _responseError = function(rejection) {
            //error her for example server respond with 401
         return $q.reject(rejection);
     InterceptorServiceFactory.request = _request;
    InterceptorServiceFactory.responseError = _responseError;
     return InterceptorServiceFactory;


then register the interceptor.

.config(["$httpProvider", function ($httpProvider) {

Every request coming will be passed her.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download