Javascript 在AngularJS$resource模块上发出请求之前获取URL
我正在使用Angular JS和$resource模块使用REST api。我必须使用私钥对URL进行编码,并将编码过程的结果发送到标头上。我正在尝试拦截请求并获取请求的URL,但我无法做到这一点Javascript 在AngularJS$resource模块上发出请求之前获取URL,javascript,angularjs,Javascript,Angularjs,我正在使用Angular JS和$resource模块使用REST api。我必须使用私钥对URL进行编码,并将编码过程的结果发送到标头上。我正在尝试拦截请求并获取请求的URL,但我无法做到这一点 return $resource(url, {}, { get: { method: 'GET', headers: headers, transformRequest: function(data, headersGetter) { // Here "data" is und
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
}
});
有什么帮助吗?听起来你想要的是一个拦截器。查找的拦截器部分。它看起来像这样:
// 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');
}]);
然后像这样注册拦截器:
// 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');
}]);