Я пытаюсь перехватить 401 ответ, когда попал в API, но это было разочаровывающе не работает вообще.Не могу перехватить 401 Угловая
Когда я нахожусь на определенной ссылке, в консоли браузера, я вижу, что она отвечает с помощью 401. В принципе, я хочу перенаправить ее на мою страницу входа, когда она встречает такие ответы. Я видел здесь много вопросов и пытался сделать что-то подобное, но он не работает.
Это содержимое файла main.js.
var app = angular.module('strelsau_client', ['ngRoute', 'ngResource', 'angularCSS']).config(function ($provide, $httpProvider) {
// Intercept http calls.
$provide.factory('MyHttpInterceptor', function ($q) {
return {
// On request success
request: function (config) {
console.log(config); // Contains the data about the request before it is sent.
// Return the config or wrap it in a promise if blank.
return config || $q.when(config);
},
// On request failure
requestError: function (rejection) {
console.log(rejection); // Contains the data about the error on the request.
// Return the promise rejection.
return $q.reject(rejection);
},
// On response success
response: function (response) {
console.log(response); // Contains the data from the response.
// Return the response or promise.
return response || $q.when(response);
},
// On response failture
responseError: function (rejection) {
console.log(rejection); // Contains the data about the error.
// Return the promise rejection.
return $q.reject(rejection);
}
};
});
// Add the interceptor to the $httpProvider.
$httpProvider.interceptors.push('MyHttpInterceptor');
});
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/bookings/fare.html',
controller: 'BookingsCtrl',
css: ['../assets/css/stylesheet.css']
})
.when('/admin',{
templateUrl:'views/admin/index.html',
controller: 'AdminCtrl',
css: ['../assets/css/vendor.css','../assets/css/flat-admin.css']
})
.when('/admin/taxis',{
templateUrl:'views/admin/taxis.html',
controller: 'AdminCtrl',
css: ['../assets/css/vendor.css','../assets/css/flat-admin.css']
})
.when('/admin/customers',{
templateUrl:'views/admin/customers.html',
controller: 'AdminCtrl',
css: ['../assets/css/vendor.css','../assets/css/flat-admin.css']
})
.when('/admin/set_price', {
templateUrl: 'views/admin/set_price.html',
controller: 'AdminCtrl'
})
.when('/admin/adddriver', {
templateUrl: 'views/admin/add_taxi_driver.html',
controller: 'AdminCtrl'
})
.when('/signup', {
templateUrl: 'views/signup.html',
controller: 'AccountCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Вы не забыли проверить ошибку 401 в * responseError *, чтобы перенаправить? Примечание: вы можете использовать $ httpProvider.interceptors.push ({[object]]) вместо создания фабрики внутри угловой, которая не имеет другой цели. – Walfrat
@Walfrat Извините, но я действительно не понимаю, что вы говорите. Ни один из console.log ничего не показывает. –
try '$ httpProvider.interceptors.push ({responseError: function (rejection) {console.log (rejection); return $ q.reject (rejection);}});' – Walfrat