How read the URL and parameter from HTTP interceptor $httpProvider in angular js

那年仲夏 提交于 2019-12-02 21:59:41

问题


where i can get the URL string and the parameter of a POST request from the HttpIntercepter and push that information to IndexedDB. But i have no idea how to get the URL and Parameter from the httpintercepter.

angular.module("app").config(['$httpProvider', function ($httpProvider) {
        if (!$httpProvider.defaults.headers.get) {
            debugger;
            $httpProvider.defaults.headers.get = {};
            console.log('POST method triggered');
        }
        else {
        }
        $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
        $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
        $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
    }]);

Post call am making is to a API hosted in different server

  var tfscapacity = 'https://server.com/folderName/Folder/folderService/folderService.svc/GetResourceCapacityPlan';
        var params = {
            teamsite: 'team',
            project: 'pjt',
            iterationId: 'Sprint-3',
            team: 'team',
            username: 'UserName',
            password: 'PassWord'
        };

        $http.post(tfscapacity, params, null).then(function (response) {
        });  

回答1:


After few searching i have found the answer. You have to write a filter and inject that to http

$httpProvider.interceptors.push('myHttpInterceptor');

Please find the full code

angular.module("app").config(['$httpProvider', function ($httpProvider) {
        if (!$httpProvider.defaults.headers.get) {
            $httpProvider.defaults.headers.get = {};
            $httpProvider.interceptors.push('myHttpInterceptor');
        }
        else {
        }
        $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
        $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
        $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
    }
]);

angular.module('app').factory('myHttpInterceptor', function ($q) {
    return {
        // optional method
        'request': function (config) {
            debugger;
            return config;
        },
        // optional method
        'requestError': function (rejection) {
            // do something on error
            return $q.reject(rejection);
        },
        // optional method
        'response': function (response) {
            // do something on success
            return response;
        },
        // optional method
        'responseError': function (rejection) {
            // do something on error
            return $q.reject(rejection);
        }
    };
});


来源:https://stackoverflow.com/questions/52171297/how-read-the-url-and-parameter-from-http-interceptor-httpprovider-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!