2015-10-09 35 views
1
var app = angular.module('app'); 
// register the interceptor as a service 
app.factory('myHttpInterceptor', function($q) { 
    return { 

     'request': function(config) { 
      return config; 
     } 
    }; 
}); 

我想修改API調用的網址和API URL追加到攔截Ajax調用的開始insted的各項服務功能的像

function getAssesmentResults(params) { 

     return $http.get(url.api + '/results', {params: params}) 
      .then(function(response) { 
       return response.data; 
      }); 
    } 

但是攔截器攔截所有http請求,例如.css或.html或.json文件。在不修改其他http請求的情況下修改攔截器中的urls的好方法是什麼?

+0

是否所有的API調用都有一些共同點? –

回答

3

$http has a facility to intercept and rewrite URLs我s配置在$httpProvider上。當我在生產環境中運行時,我有一個額外的令牌:'/ rest /'與開發模式相比,並且我在攔截器中檢測生產模式(前綴packing)。這是在我的app.js

var rest_srvr = 'http://dev-pc.example.com:8000' 
app.factory('REST_Interceptor',[ 
     '$location', 
function($location) { 
    var request = function(config) { 
    if (RegExp('packing','i').test(window.location.host)) { 
     return config 
    } 
    var rest_request_regex = new RegExp('^.*?/rest/(.*)$') 
    //console.log('config.url=%s',config.url) 
    config.url = config.url.replace(rest_request_regex,rest_srvr+'/$1') 

    var files_request_regex = new RegExp('^/(files/(.*))$') 
    config.url = config.url.replace(files_request_regex,rest_srvr+'/$1') 
    //console.log(' is now config.url=%s',config.url) 
    return config 
    } 

    var translate_subpath = function(subpath) { 
    return request({url:'https://'+$location.host()+subpath}).url 
    } 

    return { 
    request: request, 
    translate_subpath: translate_subpath 
    } 
}]) 

app.config([ 
      '$httpProvider','$cookiesProvider', 
    function($httpProvider, $cookiesProvider) { 
    if (!RegExp('packing','i').test(window.location.host)) { 
     $httpProvider.interceptors.push('REST_Interceptor') 
    } 
}]) 
+0

如果我錯了,請糾正我。所以我相信你建議使用這樣的東西。所以基本上我會將api url路徑附加到api調用,但對靜態資源的調用不會被修改if(response.config.url.startsWith('/ api /')){ //在此處執行您的自定義處理 } –

+0

究竟。這就是'var files_request_regex = new RegExp('^ /(files /(.*))$')'和'config.url = config.url.replace(files_request_regex,rest_srvr +'/ $ 1')''的行。 –

1

我會創建一個包裝$http的服務。然後在你的代碼中,你總是會調用這個包裝而不是$http,並且可以在發送之前對請求做任何你想做的事情。只是一個簡單的例子:

module.factory('myHttp', function($http){ 
    return { 
    get: function(url, params){ 
     var newUrl = "base-api-url/" + url; 
     return $http.get(newUrl, params); 
    } 
    } 
}) 
0

使用這種通用服務:

通用服務

appCless.factory("$comum", function($http, $q, $injector) { 
    function ajax(url, parametros, metodo) { 
     var requisicao = $http({ 
      method: metodo, 
      url: url, 
      data:parametros 
     }); 

     var promessa = requisicao.then(
      function(resposta) { 
       return(resposta.data); 
      }, 
      function(resposta) { 
       return($q.reject("Something went wrong")); 
      } 
     ); 
     return promessa; 
    } 
    return({ 
     ajax:ajax 
    }); 
}); 

服務

app.factory("$categoriaproduto", function($comum) { 
    var categoria; 
    return { 
     buscar : function(neoId) { 
      var promessa = $comum.ajax("/fusion/services/roi/category/product/search", "", "POST"); 
      promessa.then(function(req) { 
       categoria = req.data; 
      }); 
      return promessa; 
     }, 
     cache : function() { 
      return categoria; 
     } 
    }; 
});