AngularJs routeProvider http状态403 [英] AngularJs routeProvider http status 403

查看:183
本文介绍了AngularJs routeProvider http状态403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在服务器端进行身份验证和授权。

I'm doing authentication and authorization in the server side.

在angularJs中,我正在使用routeProvider这样做路由。

In angularJs I'm doing the routing using the routeProvider like this.

$routeProvider.
        when('/', {
            templateUrl: 'partials/_home',
            controller: 'HomeCtrl'
        }).
        when('/home', {
            templateUrl: 'partials/_home',
            controller: 'HomeCtrl'
        }).
        when('/users', {
            templateUrl: 'partials/_users',
            controller: 'UserCtrl'
        }).
        when('/users/:id', {
            templateUrl: 'partials/_userForm',
            controller: 'UserCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });

这是要解决的问题,当我得到403角度没有显示服务器页面时,它只是没有做任何事情。

And here is the problem to solve, when I get a 403 angular is not showing the server page, it just does not do nothing.

有人建议如何处理这个吗?

Did some one has a suggestion of how to handle this?

推荐答案

AngularJS拦截器 - 更新到v1.4.2



AngularJS Interceptors - updated to v1.4.2


拦截器是通过将$ httpProvider添加到$ httpProvider.interceptors数组中的$ httpProvider注册的服务工厂。调用工厂并注入依赖项(如果指定)并返回拦截器。

The interceptors are service factories that are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. The factory is called and injected with dependencies (if specified) and returns the interceptor.

要了解更多信息: $ http angularjs Doc

.config(function ($httpProvider) {
    $httpProvider.interceptors.push('responseObserver');
})



响应 - 观察员工厂



403.html 500.html 是现有的HTML文件,有一些很好的帮助用户的内容。

Response - observer factory

403.html and 500.html are existing HTML files, nice styled with some help content for user.

.factory('responseObserver', function responseObserver($q, $window) {
    return {
        'responseError': function(errorResponse) {
            switch (errorResponse.status) {
            case 403:
                $window.location = './403.html';
                break;
            case 500:
                $window.location = './500.html';
                break;
            }
            return $q.reject(errorResponse);
        }
    };
});

扩展有关拦截器的知识: http://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/

To extend knowledge about interceptors: http://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/

这篇关于AngularJs routeProvider http状态403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆