在转换请求函数中获取$http选项

Get $http options in transorm request function

本文关键字:http 选项 获取 转换 请求 函数      更新时间:2023-09-26

我在AngularJS应用程序中有一个请求转换函数。根据传递给$http的参数,我需要在请求发送之前执行不同的操作。但似乎请求转换函数只获得数据作为输入。有没有一种方法可以在Angular中完成它,并且只使用标准的$http ?

使用请求拦截器:

app.config(['$httpProvider', function($httpProvider){
    var myHttpInterceptor = ['$rootScope', '$q', function($rootScope, $q){
        return {
            'request': function(config) {
                // config is an object you passed to $http(config);
                // you are free to modify config or return a new one
                if(config./*your logic*/){
                    // I want to perform an action depending on $http options. But I want to do it before request is performed
                    // + you can return $q.reject('reason') if you want to stop this http request with rejetion
                    // + you can return a promise that resolves with config object. http request will continue when this promise resolves or is rejected
                }
                return config || $q.when(config);
            }
        };
    }];
    $httpProvider.interceptors.push(myHttpInterceptor);
}]);

更多信息搜索拦截器段落https://docs.angularjs.org/api/ng/service/$http