2015-03-29 26 views
6

'un 'üstbilgileri' özelliği okunamıyor Bu bir Hata alıp, bir AJAX Döndürücü yükleme kodunu uygulamaya çalışırken bilinmeyen nedenlerle alıyorum.AngularJS Interceptor TypeError: Tanımlanmamış

Başlığın nerede tanımlanması gerektiğini anlamıyorum. console.log(config) yaptım ama orada headers: accept: text/html değerini görebiliyorum. Eğer (daha iyi kod okunabilirliği için hizmette $ rootScope sarma) önleyicilerin kullanarak bir değer değiştirici nasıl uygulanacağı konusunda tam bir örnek var Burada

/** 
* Spinner Service 
*/ 

//Spinner Constants 
diary.constant('START_REQUEST','START_REQUEST'); 
diary.constant('END_REQUEST','END_REQUEST'); 

//Register the interceptor service 
diary.factory('ajaxInterceptor', ['$injector','START_REQUEST', 'END_REQUEST', function ($injector, START_REQUEST, END_REQUEST) { 
    var $http, 
    $rootScope, 
    myAjaxInterceptor = { 
     request: function (config) { 
      $http = $http || $injector.get('$http'); 
      if ($http.pendingRequests.length < 1) { 
       console.log(config); 
       $rootScope = $rootScope || $injector.get('$rootScope'); 
       $rootScope.$broadcast(START_REQUEST); 
      } 
     } 
    }; 

    return myAjaxInterceptor; 
}]); 

diary.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.interceptors.push('ajaxInterceptor'); 
}]); 
+0

bu hatayı hangi satır? – Grundy

+0

Hayır Hattı Numarası ::: ====== açısal TypeError tarafından gösteri: processQueue (angularjs en : $ get.serverRequest (9366 angularjs) de tanımlanmamış malı 'başlıklarını' okunamadı: 13248) angular.js'de: 13264 , Scope. $ Get.Scope. $ Eval (angular.js: 14466) Scope'da , Scope. $ Get.Scope. $ Digest (angular.js: 14282) . get.Scope. $ apply (angular.js: 14571) bootstrapApply (angular.js: 1455) at object.invoke (angular.js: 4203) doBootstrap'te (angular.js: 1453) önyükleme sırasında (açısal .js: 1473) – ChanX

cevap

0

:

Aşağıda benim Kod olduğunu.

http://lemoncode.net/2013/07/31/angularjs-found-great-solution-to-display-ajax-spinner-loading-widget/

Sizin de belirttiğiniz gibi, bu itiraz edildi, (iç kodu basitleştirilmiş) kullanıyorum şimdiki yapı (I yazı güncellemek için var). Ben en iyi bir plunker başlamak olabileceğini düşünüyorum, belki de tou uyguladıklarını yolu ile ilgisi yoktur

myapp.factory('httpInterceptor', ['$q', '$injector', 
    function ($q, $injector) { 

    return { 
     'request': function(config) { 
     // request your $rootscope messaging should be here? 
     return config; 
     }, 

    'requestError': function(rejection) { 
     // request error your $rootscope messagin should be here? 
     return $q.reject(rejection); 
     }, 


     'response': function(response) { 
     // response your $rootscope messagin should be here? 

     return response; 
     }, 

    'responseError': function(rejection) { 
     // response error your $rootscope messagin should be here? 

     return $q.reject(rejection); 

     } 
    }; 
    } 
]); 
+0

atıfta bulunulan öğretici, blog yazısının içinde güncel değil .. Aslında yazıyı takip ettim ilk ama daha sonra $ httpProviders.responseInterceptors kullanımdan kaldırılmış olduğunu öğrendim .. Ben köşeli 1.3.15 – ChanX

+0

Mmm kullanıyorum ... Bu konuda üzgünüm, daha güncel projelerdeki kodu kontrol etmek $ httpProvider.interceptors kullanıyorum. push ('httpInterceptor') ;, ve bu yöntemin "public" yöntemleri "talebi", requestError "ve" reponseror "var, bu hizmeti yapıştırmamı ister misiniz? – Braulio

+0

Bu yöntemi denedim .. Aslında yukarıdaki yöntemime dayanıyor .. Ama tanımsız hataların mülkiyet başlıkları .. Her neyse bu hizmeti yapıştırabilirsiniz .. – ChanX

12

(Bana bir tohum plunkr arama yapması) Ben çözüm buldum.

Bir önleme tam olarak senin (https://docs.angularjs.org/api/ng/service/$http#interceptors)

kısaltmak için gibi bir önleme yakalayacağım, yapılandırma aynı tanımlanmış ve bunu iade etmek zorunda olan bir angularjs proje kapsamında aynı sorunu yaşadım. Ve sen unuttun.

Yani şöyle olacaktır: on

request: function (config) { 
    $http = $http || $injector.get('$http'); 
    if ($http.pendingRequests.length < 1) { 
     $rootScope = $rootScope || $injector.get('$rootScope'); 
     $rootScope.$broadcast(START_REQUEST); 
    } 
    return config; 
}