2013-11-26 30 views
0

我正在使用$ resource模块使用Angular JS来使用REST API。我必须使用私钥对URL进行编码,并将编码过程的结果发送到标头。我试图拦截请求并获取请求的URL,但我无法做到这一点。

return $resource(url, {}, { 
    get: { method: 'GET', headers: headers, transformRequest: function(data, headersGetter) { 
     // Here "data" is undefined. headersGetter() returns the headers. 
     // I need the URL here 
    } 
}); 

任何帮助?

回答

1

这听起来像你想要的是拦截器。找到拦截器部分的$http documentation。这将是这个样子:

// register the interceptor as a service 
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) { 
    return { 
     // optional method 
     'request': function(config) { 
      //modify headers in config based on url in config 
      return config; 
     }, 
    } 
}); 

然后注册拦截器是这样的:

app.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.interceptors.push('myHttpInterceptor'); 
}]);