本文介绍了如何使用angularJS拦截器拦截只有特定的HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道如何截获所有请求,但我只是想从我的资源截获的请求。

有谁知道如何做到这一点?

  services.config(['$ httpProvider',函数($ httpProvider){
    $ httpProvider.interceptors.push('myHttpInterceptor');
}]);services.factory(userPurchased功能($资源){
    返回$资源(/ API /用户/购买/:动作/:项,
        {},
        {
            名单:{方法:GET,则params:{行动:'名单'},IsArray的:假},
            保存:{方法:把',则params:{项目:@item'}},
            删除:{方法:删除,则params:{项目:@item'}},
        }
    );
});services.factory('myHttpInterceptor',函数($ Q $ rootScope){
    // $ rootScope.showSpinner = FALSE;
    返回{      回应:功能(响应){
        $ rootScope.showSpinner = FALSE;
        //做事情成功
        的console.log(成功);
        的console.log('状态',response.status);
        //返回响应;
        返回响应|| $ q.when(响应);
      },     responseError:功能(响应){
        //做一些错误
        $ rootScope.showSpinner = TRUE;
        的console.log(失败);
        的console.log('状态',response.status)
        //返回响应;
        返回$ q.reject(响应);
      }
    };
  });


解决方案

如果您想截取从特定的资源只能请求,您可以使用<$可选拦截属性C $ C> $请求的行动。角的文档(用法>动作)

的JavaScript

  angular.module('应用',['ngResource'])。
  工厂('resourceInterceptor',函数(){
    返回{
      回应:功能(响应){
        的console.log('响应拦截:',响应);
      }
    }
  })。
  工厂('resourceService',['$资源','resourceInterceptor',函数($资源,resourceInterceptor){
    返回$资源(:名称,
        {},
        {
            名单:{方法:GET,IsArray的:假的,拦截器:resourceInterceptor}
        }
    );
  }])。
  运行(['resourceService','$ HTTP',函数(resourceService,$ HTTP){
    resourceService.list({名称:'list.json'}); //&LT; =截获
    $ http.get('list.json'); //&LT; =不拦截
  }]);

Plunker:<一href=\"http://plnkr.co/edit/xjJH1rdJyB6vvpDACJOT?p=$p$pview\">http://plnkr.co/edit/xjJH1rdJyB6vvpDACJOT?p=$p$pview

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(['$httpProvider',function($httpProvider) {
    $httpProvider.interceptors.push('myHttpInterceptor');
}]);

services.factory("userPurchased", function ($resource) {
    return $resource("/api/user/purchases/:action/:item",
        {},
        {
            'list': {method: 'GET', params: {action: 'list'}, isArray: false},
            'save': {method: 'PUT', params: {item: '@item'}},
            'remove': {method: 'DELETE', params: {item: '@item'}},
        }
    );
});

services.factory('myHttpInterceptor', function($q,$rootScope) {
    // $rootScope.showSpinner = false;
    return {

      response: function(response) {
        $rootScope.showSpinner = false;
        // do something on success
        console.log('success');
        console.log('status', response.status);
        //return response;
        return response || $q.when(response);
      },

     responseError: function(response) {
        // do something on error
        $rootScope.showSpinner = true;
        console.log('failure');
        console.log('status', response.status)
        //return response;
        return $q.reject(response);
      }
    };
  });
解决方案

If you want to intercept only requests from specific resources, you can use optional interceptor property of $request action. Angular's documentation see here (Usage>actions)

JavaScript

angular.module('app', ['ngResource']).
  factory('resourceInterceptor', function() {
    return {
      response: function(response) {
        console.log('response intercepted: ', response);
      }
    }
  }).
  factory('resourceService', ['$resource', 'resourceInterceptor', function($resource, resourceInterceptor) {
    return $resource(":name",
        {},
        {
            'list': {method: 'GET', isArray: false, interceptor: resourceInterceptor}
        }
    );
  }]).
  run(['resourceService', '$http', function(resourceService, $http) {
    resourceService.list({name: 'list.json'}); // <= intercepted
    $http.get('list.json'); // <= not intercepted
  }]);

Plunker: http://plnkr.co/edit/xjJH1rdJyB6vvpDACJOT?p=preview

这篇关于如何使用angularJS拦截器拦截只有特定的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:51