2013-09-24 7 views
15

Facciamo impostare un semplice esempio:

$scope.whatDoesTheFoxSay = function(){ 
    $http.post("/backend/ancientMystery", { 
... 

Come posso trasformare a livello globale l'URL in cui la richiesta POST viene inviata a? Essenzialmente voglio anteporre un URL ad ogni richiesta http.

Quello che ho provato è impostare una variabile nel $rootScope contenente l'url all'avvio dell'applicazione. Ma questo non è quello che voglio il mio codice a guardare come:

$scope.whatDoesTheFoxSay = function(){ 
    $http.post($rootScope.backendUrl + "/backend/hidingDeepInTheWoods", { 
... 

Ho ragione supponendo che dovrei guardare in $httpProvider.defaults.transformRequest? Qualcuno può fornirmi qualche codice di esempio di base?

risposta

37

Ho un altro approccio di utilizzare richiesta intercettore con $ http che gestirà tutte le url ad un luogo comune

<!doctype html> 
<html ng-app="test"> 
    <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script> 

    </head> 
<body ng-controller="test" >  


<!-- tabs --> 


<script> 
    var app = angular.module('test', []); 
    app.config(function ($httpProvider) { 
     $httpProvider.interceptors.push(function ($q) { 
      return { 
       'request': function (config) { 
        config.url = config.url + '?id=123'; 
        return config || $q.when(config); 

       } 

      } 
     }); 
    }); 

    app.controller('test', function ($scope,$http) { 
     $http.get('Response.txt').success(function (data) { alert(data) }).error(function (fail) { 

     }); 
    }); 

    </script> 
</body> 


</html> 
+0

Quando si utilizza questo insieme a 'ngToast' ottengo l'errore' Errore: [$ compilazione: tplrt] Modello per direttiva 'brindisi' deve avere esattamente una radice element' –

+2

'' 'config.url = config. url + '? id = 123'; return config || $ q.when (config); '' 'Subito dopo l'accesso a' '' config.url''', cosa renderebbe '' 'config''' su false? –

0

imbattuto in questo problema di "cache-busting in AngularJS" e ha voluto condividere un gruppo di lavoro soluzione che include anche un'opzione per "annullare la cache" delle risorse $templatecache.

Questa soluzione restituisce correttamente un valore e non una promessa ;) e non forma URL non corretti se la richiesta contiene già valori $_GET.

var __version_number = 6.0; // Date.now('U'); // 'U' -> linux/unix epoch date int 

app.config(['$httpProvider', function($httpProvider) { 
    $httpProvider.interceptors.push(function() { 
    return { 
     'request': function (config) { 
     // !!config.cached represents if the request is resolved using 
     //  the angular-templatecache 
     if (!config.cached) { 
      config.url += ((config.url.indexOf('?') > -1) ? '&' : '?') 
      + config.paramSerializer({v: __version_number}); 
     } else if (config.url.indexOf('no-cache') > -1) { 
      // if the cached URL contains 'no-cache' then remove it from the cache 
      config.cache.remove(config.url); 
      config.cached = false; // unknown consequences 
      // Warning: if you remove the value form the cache, and the asset is not 
      //   accessable at the given URL, you will get a 404 error. 
     } 
     return config; 
     } 
    } 
    }); 
}]); 
Problemi correlati