您好,我正在使用ngResource $save方法,我得到了两种不同的行为,我不明白为什么
首先,我这样使用它:
$scope.user = new User($scope.user);
$scope.user.$save(function () {
$window.location.href = //redirection here;
}, function (response) {
$scope.form.addErrors(response.data.errors);
});然后,当我执行类似的操作时,我有了另一个控制器,但即使从服务器获得404或422个错误,第一个回调也会被执行,错误回调会被忽略。
有谁知道这一点吗?我已经在谷歌上搜索了几个小时,试图找到更多关于$save的文档,但我仍然被这个问题困扰着。
谢谢。
发布于 2014-04-08 02:56:10
好吧,问题出在我用来检测401 (未授权错误)的拦截器上。
这里是拦截器,注意您必须返回$q.reject(response),否则其他回调不会被调用(在我的例子中是ngResource.$save中的错误回调)
MyApp.config(function ($httpProvider) {
$httpProvider.interceptors.push(function($window, $q) {
return {
'responseError': function(response) {
if (response.status == 401) { // Unathorized
$window.location.href = 'index.html';
}
// return response; <-- I was doing this before cancelling all errors
return $q.reject(response);
}
};
});
});https://stackoverflow.com/questions/22917075
复制相似问题