我是AngularJS的新手,现在花了3天时间找到处理401状态的方法.我尝试过使用$ http的拦截器,使用$ resource ...但没有任何工作.我的应用程序在同一台服务器上调用JSONP调用.当发生错误时,它会被错误回调函数捕获.但状态始终为0且响应未定义.
首先,我尝试了这个拦截器
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push(['$q', function($q) {
return function(promise) {
return promise.then(function(response) {
console.log('success in interceptor');
return response;
}, function(response) {
console.log('error in interceptor');
console.log(response);
if (response.status === 401) {
response.data = {
status: false,
description: 'Authentication required!'
};
return response;
}
return $q.reject(response);
});
}
}]);
}]);
Run Code Online (Sandbox Code Playgroud)
其次,还尝试使用$ resource的控制器
$scope.fetchData = function(fromDate, toDate){
Cancel.get({from: fromDate, to: toDate, perPage: 99999},
function(data){
$scope.cancels = $scope.filteredCancels = data.data;
$scope.search();
},
function(response) {
$scope.errorMessage = '<h4>Error : '+response.status+'</h4>';
window.location …
Run Code Online (Sandbox Code Playgroud)