gpt4 book ai didi

javascript - Angular JS拦截器检查响应数据返回html

转载 作者:行者123 更新时间:2023-11-30 11:41:13 24 4
gpt4 key购买 nike

我正在尝试在 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 拦截器的 API 响应

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

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

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

最佳答案

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

InterceptorServiceFactory.responseError = _response;

看看this answer了解更多详情

关于javascript - Angular JS拦截器检查响应数据返回html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42586993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com