错误:未定义承诺

时间:2015-04-08 05:24:51

标签: angularjs angular-promise

我有一个工厂,其功能是处理重定向状态代码。如果它看到状态代码是302,它会将页面重定向到登录页面。

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');
}]);

但是一旦服务器返回302,我就会收到错误。这里是

  

错误:未定义承诺

我做错了什么?

1 个答案:

答案 0 :(得分:-2)

你需要使用拦截器 有时您可能需要修改HTTP请求和响应。这可能是由于各种原因,例如为HTTP错误添加全局逻辑处理。使用拦截器,您可以在Angular应用程序中轻松完成此任务。

请参阅:https://egghead.io/lessons/angularjs-using-angularjs-interceptors-with-http

另请参阅此类似帖子AngularJS using an interceptor to handle $http 404s - promise not defined error