【问题标题】:Angular JS Interceptor checking for response data returns htmlAngular JS拦截器检查响应数据返回html
【发布时间】:2017-03-03 19:31:56
【问题描述】:

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

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

这是我来自 Spring Interceptor 的 API 响应

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

我正在尝试检查我的回复并检查代码,如果代码匹配,我正在尝试将他带到另一个屏幕。但是,每当我在控制台内部数据上看到响应时,它就会有 Html 。

当我分析“网络”选项卡时,我可以看到 API 调用失败,响应为 401。并且所有其他的 HTML 已经加载完毕,同时加载了 html。响应数据来自 Html 。那么我的拦截器中缺少什么?

【问题讨论】:

    标签: javascript html angularjs intercept


    【解决方案1】:

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

    InterceptorServiceFactory.responseError = _response;
    

    查看this answer了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2018-06-27
      • 1970-01-01
      • 2015-11-13
      相关资源
      最近更新 更多