2016-02-02 8 views
-1

Я хочу, чтобы весь запрос выполнялся из моего приложения на одной странице, например, на другой вкладке или любой гиперссылке.Перехватчик AngularJS

Я написал перехватчик и хочу поместить вызов ajax для всех запросов.

.factory('httpRequestInterceptor', function($q,$http){ 
    return { 
     request: function($http,config){ 
      window.alert(config.url);    
      var dummyValue = $http.get("url"); 
      return config;  
     }      
    } 
}) 
.config(function($httpProvider) { 
    $httpProvider.interceptors.push('httpRequestInterceptor'); 
}); 
+2

У вас есть вопрос или это только список требований? – Claies

+0

Можем ли мы помещать фиктивный вызов ajax внутри запроса перехватчика? –

ответ

0

Я думаю, что вы находитесь в поисках http-logger типа фабрики. Вы можете использовать этот

.config(function ($provide, $httpProvider) { 

    // Intercept http calls. 
    $provide.factory('MyHttpInterceptor', function ($q) { 
     return { 
      // On request success 
      request: function (config) { 
       console.log(config); // Contains the data about the request before it is sent. 

       // Return the config or wrap it in a promise if blank. 
       return config || $q.when(config); 
      }, 

      // On request failure 
      requestError: function (rejection) { 
       console.log(rejection); // Contains the data about the error on the request. 

       // Return the promise rejection. 
       return $q.reject(rejection); 
      }, 

      // On response success 
      response: function (response) { 
       console.log(response); // Contains the data from the response. 

       // Return the response or promise. 
       return response || $q.when(response); 
      }, 

      // On response failture 
      responseError: function (rejection) { 
       console.log(rejection); // Contains the data about the error. 

       // Return the promise rejection. 
       return $q.reject(rejection); 
      } 
     }; 
    }); 

    // Add the interceptor to the $httpProvider. 
    $httpProvider.interceptors.push('MyHttpInterceptor'); 

}); 

Я тестировал, он также работает для навигации по маршруту. Для получения дополнительной информации, пожалуйста, проверьте этот блог https://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/

+0

С внешнего интерфейса вы можете записывать журналы только на консоль, а не на журналы IIS. Но да, вы теперь перехватываете каждый запрос ответа, и вы можете использовать эти методы для работы с аналитическим инструментом. –

Смежные вопросы