0

I have a factory and its function is to handle redirect status codes. If it sees that the status code is 302, it will redirect the page to the login page.

app.factory('redirectInterceptor', function($q,$location){
    return {
        responseError : function(response){
            return promise.then(
                function success(response) {
                    if(response.status === 302){
                        alert("success  " +response.status);
                        $location.path('/login.html');
                        return response;
                    }
                    else{
                        alert("success  "  +response.status);
                        return response; 
                    }
                },
                function error(response) {
                    if(response.status === 302){
                        alert("error " +response.status);
                        $location.path('/public/login.html');
                        return $q.reject(response);
                    }
                    else{
                        alert("error  " +response.status);
                        return $q.reject(response); 
                    }
                }
            );
        }
    }
});

app.config(['$httpProvider',function($httpProvider) {
    $httpProvider.interceptors.push('redirectInterceptor');
}]);

But I get an error once the server returns 302. here it is

Error: promise is not defined

What have I done wrong?

user3714598
  • 1,733
  • 5
  • 28
  • 45

1 Answers1

-2

You need to use interceptors Sometimes you might need to modify HTTP requests and responses. This could be for a variety of reasons such as adding global logic handling for HTTP errors. With interceptors, you can easily accomplish this in your Angular applications.

Refer this: https://egghead.io/lessons/angularjs-using-angularjs-interceptors-with-http

Also see this similar post AngularJS using an interceptor to handle $http 404s - promise not defined error

Community
  • 1
  • 1
Reena
  • 1,109
  • 8
  • 16