0

你好我正在使用 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但我仍然遇到这个问题。

谢谢你。

4

1 回答 1

0

好吧,问题出在我用来检测 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);
            }
        };
    });
});
于 2014-04-07T18:56:09.997 回答