Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS$resource模块上发出请求之前获取URL_Javascript_Angularjs - Fatal编程技术网

Javascript 在AngularJS$resource模块上发出请求之前获取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

我正在使用Angular JS和$resource模块使用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
    } 
});

有什么帮助吗?

听起来你想要的是一个拦截器。查找的拦截器部分。它看起来像这样:

// 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');
}]);