How to use angularJS interceptor to only intercept specific http requests?

前端 未结 7 564
耶瑟儿~
耶瑟儿~ 2020-12-23 21:17

I know how to intercept ALL requests, but I only want to intercept requests from my resources.

Does anyone know how to do this?

services.config([\'$h         


        
相关标签:
7条回答
  • 2020-12-23 21:55

    My preferred way is to use an HTTP interceptor which replaces a "magic" Authorization header with the current OAuth token. The code below is OAuth specific, but remedying that is a simple exercise for the reader.

    // Injects an HTTP interceptor that replaces a "Bearer" authorization header
    // with the current Bearer token.
    module.factory('oauthHttpInterceptor', function (OAuth) {
      return {
        request: function (config) {
          if (config.headers.Authorization === 'Bearer') {
            config.headers.Authorization = 'Bearer ' + btoa(OAuth.accessToken);
          }
          return config;
        }
      };
    });
    
    module.config(function ($httpProvider) {
      $httpProvider.interceptors.push('oauthHttpInterceptor');
    });
    
    0 讨论(0)
提交回复
热议问题