2017-03-03 55 views
2

我想在Angular JS端实现一个拦截器,为此我将检查特定的响应并将用户导航到其他页面。这是我的拦截Angular JS拦截器检查响应数据返回html

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

var _request = function(config){ 
    console.log('Coming inside'); 
    console.log(config); 
    return config; 
} 

var _response = function(response){ 
    console.log('Coming inside for Result'); 
    console.log(response); 
    if (response.data.code == "USER_TIMEDOUT") { 
     window.location.href = '/unrestricted/jsp/FormLogin.jsp'; 
     alert(worked); 
    } 
    return response; 
} 

InterceptorServiceFactory.request = _request; 
InterceptorServiceFactory.response = _response; 
return InterceptorServiceFactory; 
}]); 

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

这是春天拦截我的API响应

{ 
"code": "USER_TIMEDOUT", 
"message": "User session timedout for requestUri=/services/search/Employee" 
} 

我想检查我的反应和检查代码,如果代码相匹配,我试图把oput他到另一个屏幕。但是每当我在控制台内部看到数据中的响应时,就会看到Html。

当我分析网络选项卡时,我能够看到API调用失败,响应401。而所有其他HTML已加载,而加载的HTML。响应数据进入Html。所以我在Interceptor中缺少的东西?

回答

3

由于响应代码401(超过400被认为是一个请求失败), 你需要使用

InterceptorServiceFactory.responseError = _response; 

this answer看一看了解更多详情