1

My Question is a bigger and broader version of this question. I want to intercept all http requests issued inside an AngularJS function. Later I need to alter the request URL and than pass it to the server..

How can I do that ? So far I have used $httpProvider and $q to create a interceptor but I am only able to intercept only $http requests not all the requests i.e. if someone clicks on any href link on my page etc. My interceptor code is :-

// register the interceptor as a service
myModule.factory('myHttpInterceptor', function ($q) {
    return {
        // optional method
        'request': function (config) {
            // do something on success
            console.log("request success");
            return config;
        },
        // optional method
        'requestError': function (rejection) {
            // do something on error
            console.log("Request Error");
            if (canRecover(rejection)) {
                return responseOrNewPromise
            }
            return $q.reject(rejection);
        },
        // optional method
        'response': function (response) {
            // do something on success
            console.log("Response received");
            return response;
        },
        // optional method
        'responseError': function (rejection) {
            // do something on error
            if (canRecover(rejection)) {
                return responseOrNewPromise
            }
            return $q.reject(rejection);
        }
    };
});
myModule.factory('myInterceptor', ['$log', function ($log) {
        $log.debug('$log is here to show you that this is a regular factory with injection');

        var myInterceptor = {
        };

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

1 Answer 1

1

Intercepting navigation to other pages is different from intercepting http requests. Maybe what you want is to intercept $location changes.

Have a read through this. You can do it but it depends on where the location changes are to.

http://uiadventures.wordpress.com/2013/09/06/routechange-angularjs/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.