Interceptor for all http requests in angularJS 1.0.x

后端 未结 2 1316
孤街浪徒
孤街浪徒 2021-01-01 01:59

I am currently working in a angular app in whcih I wanted to write an interceptor for all http request from my app which in turns calls a service to know whether the single

2条回答
  •  醉话见心
    2021-01-01 02:46

    You can use interceptor very easily

    Here is a sample

    var mydevices = angular.module('deviceDetails', ['ui.bootstrap', 'tags-input'])
    
    mydevices.config(function ($httpProvider) {
    $httpProvider.interceptors.push(function($q) {
          return {
           'request': function(config) {
               if (config.method === 'GET' && config.url.contains("/rest/")) {
                   var sep = config.url.indexOf('?') === -1 ? '?' : '&';
                   config.url = config.url + sep + 'cacheSlayer=' + new Date().getTime();
               }
               console.log(config.url);
               return config || $q.when(config);
            }
          };
        });
    });
    

    The example above modifies the URL for all /rest/ URLs

    Hope this helps

提交回复
热议问题