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

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

问题描述

我在服务器端做认证和授权.

在 angularJs 中,我像这样使用 routeProvider 进行路由.

$routeProvider.什么时候('/', {templateUrl: 'partials/_home',控制器:'HomeCtrl'}).当('/家',{templateUrl: 'partials/_home',控制器:'HomeCtrl'}).当('/用户',{templateUrl: 'partials/_users',控制器:'UserCtrl'}).当('/用户/:id',{templateUrl: 'partials/_userForm',控制器:'UserCtrl'}).除此以外({重定向到:'/'});

这是要解决的问题,当我得到 403 angular 没有显示服务器页面时,它什么也不做.

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

解决方案

AngularJS Interceptors - 更新至 v1.4.2

<块引用>

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

阅读更多内容:$http angularjs Doc

部分配置(部分)

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

响应 - 观察者工厂

403.html500.html 是现有的 HTML 文件,样式很好,为用户提供了一些帮助内容.

.factory('responseObserver', function responseObserver($q, $window) {返回 {'响应错误':函数(错误响应){开关(错误响应.状态){案例403:$window.location = './403.html';休息;案例 500:$window.location = './500.html';休息;}返回 $q.reject(errorResponse);}};});

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

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

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: '/'
        });

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 Interceptors - updated to v1.4.2

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.

To read more: $http angularjs Doc

Section config (part of it)

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

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);
        }
    };
});

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

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

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