SORU
15 AĞUSTOS 2012, ÇARŞAMBA


AngularJS ile hata işleyicisi küresel Ajax

Web siteme 100% jQuery iken, bunu yapmak için kullandım

$.ajaxSetup({
    global: true,
    error: function(xhr, status, err) {
        if (xhr.status == 401) {
           window.location = "./index.html";
        }
    }
});

401 hataları için küresel bir işleyicisi ayarlayın. Şimdi, $resource $http ile angularjs (DİNLENME) isteklerini yapmak için kullandığım sunucu. Benzer şekilde açısal ile genel hata işleyicisi ayarlamak için herhangi bir yolu var mı?

CEVAP
15 AĞUSTOS 2012, ÇARŞAMBA


Ayrıca açısal ile bir web sitesi inşa ediyorum ve küresel 401 kullanım için aynı engel ile karşılaştım. Bu blog yazısı rastladım http avcı ile sona erdi. Belki bunun gibi yararlı bulabilirsiniz.

"Authentication in AngularJS (or similar) based application.",espeo yazılım

EDİT: son çözüm

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives'], function ($routeProvider, $locationProvider, $httpProvider) {

    var interceptor = ['$rootScope', '$q', function (scope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status;

            if (status == 401) {
                window.location = "./index.html";
                return;
            }
            // otherwise
            return $q.reject(response);

        }

        return function (promise) {
            return promise.then(success, error);
        }

    }];
    $httpProvider.responseInterceptors.push(interceptor);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ionized Digital Air Studios

    Ionized Digi

    20 HAZİRAN 2009
  • eyes4beautee

    eyes4beautee

    17 HAZİRAN 2011
  • Jeb Corliss

    Jeb Corliss

    17 Kasım 2006