我有一個工廠,它的功能是處理重定向狀態碼。如果它看到狀態碼是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是
Error: promise is not defined
我做了什麼錯事,但我得到一個錯誤?
您的代碼的第4行的承諾從來沒有被定義。 – pixelbits
@pixelbits的含義?我必須添加這個var defer = $ q.defer(); ?然後讓它defer.promise.then ..?我一直在尋找可以幫助我的文章,我發現這個https://thinkster.io/egghead/promises – user3714598
我不認爲302是錯誤狀態碼。錯誤是4xx和5xx – Chandermani