Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs 1.3 $http interceptors

Tags:

angularjs

I've currently got the following in my app to show a loading animation whenever there are $http requests running, then hidden at the end;

app.config(['$httpProvider', function ($httpProvider) {
    var $http,
        interceptor = ['$q', '$injector', function ($q, $injector) {
            var error;

            function response(response) {
                // get $http via $injector because of circular dependency problem
                $http = $http || $injector.get('$http');
                if ($http.pendingRequests.length < 1) {
                    $('#loadingWidget').hide();
                }
                return response;
            }

            function responseError(response) {
                if (response.status == 401) {
                    alert("You have been logged out or have tried to access a restricted area, redirecting to the login screen...");
                    window.location = globals.siteUrl + 'login';
                } else {
                    // get $http via $injector because of circular dependency problem
                    $http = $http || $injector.get('$http');
                    if ($http.pendingRequests.length < 1) {
                        $('#loadingWidget').hide();
                    }
                }
                return $q.reject(response);
            }

            return function (promise) {
                $('#loadingWidget').show();
                return promise.then(response, responseError);
            }
        }];

        $httpProvider.responseInterceptors.push(interceptor);

}]);

I've been trying to convert this to work with 1.3 but I just can't seem to nail it. I've been referencing these docs $http (interceptor section) but I'm not sure how to re-write it. Can anyone help?

UPDATE: Here's what I've tried already:

app.factory('xmHttpInterceptor', function ($q, $http, $injector) {
    return {
        // optional method
        'response': function (response) {
            // get $http via $injector because of circular dependency problem
            $http = $http || $injector.get('$http');
            if ($http.pendingRequests.length < 1) {
                $('#loadingWidget').hide();
            }
            return response;
        },

        // optional method
        'responseError': function (rejection) {
            alert(rejection);
            // do something on error
            if (canRecover(rejection)) {
                return responseOrNewPromise
            }
            return $q.reject(rejection);
        }
    };
});

and:

app.config(['$httpProvider', 'xmHttpInterceptor', function ($httpProvider, xmHttpInterceptor) {
    $httpProvider.interceptors.push('xmHttpInterceptor');
}]);

But the site fails to load with:

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.0-beta.17/$injector/modulerr?p0=app&p1=Erro…s.org%2F1.3.0-beta.17%2F%24injector%2Funpr%3Fp0%3DxmHttpInterceptor%0A%20%...<omitted>...4) 
like image 427
Ben Kilah Avatar asked Jul 29 '14 03:07

Ben Kilah


People also ask

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

What is interceptor in AngularJS?

This interceptor is called when the $http receives the response from the server. This function receives a response object as a parameter return a response object or a promise. A response interceptor is used to modify the response data or adding a new set of values, calling another module or services call.

Is HTTP request is synchronous or asynchronous in AngularJS?

The problem is as follows, $http. get is asynchronous, before the response is fetched, the function returns. Therefore the calling function gets the data as empty string.

How do we pass data and get data using http in angular?

get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });


1 Answers

Since the problem was already solved on the comments, I'm posting it as a community wiki:

The $httpProvider.responseInterceptors is no longer support in 1.3, you have to use $httpProvider.interceptors instead.

-- runTarm

And:

Try not injecting your xmHttpInterceptor:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('xmHttpInterceptor');
}]);

-- David Bennett

like image 84
falsarella Avatar answered Oct 29 '22 21:10

falsarella