1

This is how I handle my routing in Angular. I have a couple of files where all the needed data about each route is stored and I iterate over the data like this. .html requests are for loading templates and .action for AJAX requests.

What I need to do is treat all the cases where the server doesn't succeed (300, 400, 500 etc). I googled all I thought relevant found some info about it but nothing seems relevant or clear enough. How exactly do I access the header data of a request?

Application.config(['$routeProvider', '$interpolateProvider', '$httpProvider',

    function($routeProvider, $interpolateProvider, $httpProvider) 
    {

        $interpolateProvider.startSymbol('<%');
        $interpolateProvider.endSymbol('%>');

        for (var i=0; i < applicationRoutes.length; i++) {

            if (applicationRoutes[i].hasInitialData)
            {
                $routeProvider.when('/' + applicationRoutes[i].path + '.html', {

                    templateUrl: applicationRoutes[i].path + '.html',
                    controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias,
                    resolve: {
                        initData : ['Initialize', '$route', function(Initialize, $route)
                        {
                            //console.log($route.current.$$route);
                            return Initialize.serverData('/' + $route.current.$$route.pathBase);
                        }]
                    },
                    pathBase: applicationRoutes[i].path

                }).otherwise('/404');
            }
            else
            {
                $routeProvider.when('/' + applicationRoutes[i].path + '.html', {

                    templateUrl: applicationRoutes[i].path + '.html',
                    controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias,
                    pathBase: applicationRoutes[i].path

                }).otherwise('/404');

            }

        }

        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

    }

]);

1 Answer 1

3

I cant see your server calls, but assuming you have rolled up a Service to call the endpoints with $http remember you can use a fail handler as the second parameter of the promise so

$http(req).then(function(){...}, function(){...});

meaning you can do this

    $http(req).then(function(someData){/*This is the success*/},function(reason){/*the reason holds the reason it faild, reason.data etc */});
Sign up to request clarification or add additional context in comments.

2 Comments

you are corrrect, however what i needed at the time was a way to access the headers of requests handled by routing. i've since then managed to handle the http ones
You can also use .catch(errorHandler) instead of the second callback in your .then().

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.