2015-03-29 21 views
6

I get questo errore per motivi sconosciuti durante il tentativo di implementare un codice di caricamento AJAX Spinner.AngularJS Interceptor TypeError: Impossibile leggere le intestazioni della proprietà di undefined

Non capisco dove si debba definire l'intestazione. Ho fatto console.log(config) ma posso vedere il valore headers: accept: text/html lì.

Qui di seguito è il mio codice:

/** 
* 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

su quale linea si ottiene questo errore? – Grundy

+0

No Line Number è spettacolo di angolare ::: ====== TypeError: Impossibile leggere proprietà 'headers' di indefinito a $ get.serverRequest (angular.js: 9366) a processQueue (angular.js: 13248) in angular.js: 13264 in Scope. $ Get.Scope. $ Eval (angular.js: 14466) in Scope. $ Get.Scope. $ Digest (angular.js: 14282) in Scope. $ get.Scope. $ apply (angular.js: 14571) in bootstrapApply (angular.js: 1455) in Object.invoke (angular.js: 4203) in doBootstrap (angular.js: 1453) all'avvio (angolare .js: 1473) – ChanX

risposta

0

Qui si ha un esempio completo su come implementare un filatore tramite intercettori (avvolgendo il $ rootScope in un servizio per una migliore leggibilità del codice).

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

Come lei ha sottolineato, questo è deprecato (devo aggiornare il post), l'attuale struttura che sto utilizzando (semplificato codice interno). Credo che la migliore potrebbe essere quella di partire da un plunker, forse non ha nulla a che fare con il modo in cui stanno attuando tou (mi permetta di cercare un plunkr seme)

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

il tutorial di riferimento all'interno del post del blog non è aggiornato .. In realtà ho seguito il post all'inizio, ma poi ho scoperto che $ httpProviders.responseInterceptors è deprecato .. Sto usando angular 1.3.15 – ChanX

+0

Mmm ... mi dispiace, controllando il codice che ho in più progetti aggiornati sto usando $ httpProvider.interceptors. push ('httpInterceptor'); e questo metodo ha "metodi" pubblici "request", requestError "e" reponseError ", vuoi che io incolli quel servizio? – Braulio

+0

Ho provato questo metodo .. In realtà il mio metodo sopra è basato su quello .. Ma intestazioni di proprietà di errore non definito .. In ogni caso si può incollare quel servizio .. – ChanX

12

Credo di avere la soluzione.

Ho avuto lo stesso problema nell'ambito di un progetto AngularJS dove un intercettore è esattamente definito uguali ai suoi (https://docs.angularjs.org/api/ng/service/$http#interceptors)

di abbreviare, un fermo intercettore la configurazione e devono restituirlo. E ti sei dimenticato di

Quindi sarebbe:

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