当angularjs有400(错误请求)时如何捕获错误?

时间:2014-06-19 06:32:17

标签: angularjs

获取网址400(错误请求)或任何获取网址错误。如何忽略错误?

$http.get(res.getQNUrl(domain, key, "exif"))
.success(function(data){
    $scope.imageExifMap[key] = data
}).error(entry.onError)

例如:http://tratao-public.qiniudn.com/c851b00c127146997f017bb899bb9bb8.jpg?exif 它会得到 {"error":"no exif data"}

Chrome获取错误:GET http://tratao-public.qiniudn.com/c851b00c127146997f017bb899bb9bb8.jpg?exif 400 (Bad Request)

1 个答案:

答案 0 :(得分:-2)

var interceptor = ['$rootScope', '$q', "Base64", function (scope, $q, Base64) {
        function success(response) {
            return response;
        }
        function error(response) {
            var status = response.status;
            if (status == 400) {
                window.location = "/account/login?redirectUrl=" + Base64.encode(document.URL);
                return;
            }
            // otherwise
            return $q.reject(response);
        }
        return function (promise) {
            return promise.then(success, error);
        }
    }];

希望这可能有所帮助!