AngularJS拦截器重定向 [英] AngularJS Interceptor to Redirect

查看:60
本文介绍了AngularJS拦截器重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ExpressJS发送以下响应...

ExpressJS is sending the following response...

res.send('ItemUploaded');

我正在尝试让AngularJS通过拦截器查看此响应并执行重定向.有没有人有示例代码,其中Angular捕获了服务器响应(例如我的"ItemUploaded")并执行了对部分响应(通过$ location)的重定向?

I'm trying to get AngularJS to see this response via an Interceptor and perform a redirect. Does anyone have sample code where Angular catches a server response (such as my "ItemUploaded") and performs a redirect to a partial (via $location)?

推荐答案

这是拦截器的工厂:

 .factory('InterceptorService',['$q', '$location', function( $q, $location, $http){
     var InterceptorServiceFactory = {};

     var _request = function(config){
         //success logic here
         return config;
     }

     var _responseError = function(rejection) {
            //error here. for example server respond with 401
         return $q.reject(rejection);
     }

     InterceptorServiceFactory.request = _request;
     InterceptorServiceFactory.responseError = _responseError;
     return InterceptorServiceFactory;

    }]);

然后注册拦截器:

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

每个请求都会在这里传递.

Every request coming will be passed here.

这篇关于AngularJS拦截器重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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