Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 错误:未定义承诺_Angularjs_Angular Promise - Fatal编程技术网

Angularjs 错误:未定义承诺

Angularjs 错误:未定义承诺,angularjs,angular-promise,Angularjs,Angular Promise,我有一个工厂,它的功能是处理重定向状态代码。如果它看到状态代码是302,它将把页面重定向到登录页面 app.factory('redirectInterceptor', function($q,$location){ return { responseError : function(response){ return promise.then( function success(response) {

我有一个工厂,它的功能是处理重定向状态代码。如果它看到状态代码是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,我就会得到一个错误。给你

错误:未定义承诺


我做错了什么?

您需要使用拦截器 有时您可能需要修改HTTP请求和响应。这可能有多种原因,例如为HTTP错误添加全局逻辑处理。使用拦截器,您可以在角度应用程序中轻松实现这一点

请参阅:


另请参阅此类似帖子

代码第4行的承诺从未定义过。@pixelbits的意思是什么?我必须添加这个变量defer=$q.defer()?那就推迟吧。答应我。然后?我一直在寻找可以帮助我的文章,我发现这个我不认为302是一个错误状态代码。错误为4xx和4xx5xx@chandermani所以,即使我使用http拦截器,我也无法获得302?我认为在302上,浏览器执行自动重定向。搜索$http和302重定向。