首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角ngResources请求拦截器

角ngResources请求拦截器
EN

Stack Overflow用户
提问于 2014-12-03 19:36:59
回答 1查看 1.8K关注 0票数 0

为什么角$resources没有请求和请求错误拦截器?

有办法吗?

Doc内容:

拦截器- {Object=} -拦截器对象有两个可选的方法-响应和responseError。响应和responseError拦截器都会被http响应对象调用。见$http拦截器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-03 19:48:30

您可以实现自己的拦截器,如下所示。

代码语言:javascript
复制
app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('myInterceptor');
});

app.factory('myInterceptor', ['$q', function ($q) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            // insert code to populate your request header for instance
            return config;
        },
        response: function (response) {
            if (response.status === 403 || response.status === 401) {
                // insert code to redirect to custom unauthorized page
            }
            return response || $q.when(response);
        }
    };
}]);

希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27280180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档