WebAPI在登录超时时向我发送了一个完整的HTML页面.如何在AngularJS中使用此响应
WebAPI is sending me a whole HTML page on login timeout. How to use this response in AngularJS?
我有以下代码来调用我正在开发的业务应用程序客户端的服务(后端有.NET WebAPI):
resource = $resource(apiEndPointManager.endpoints.masterValues, {}, {
GetByCode: {
url: '{0}/:code'.format(apiEndPointManager.endpoints.masterValues),
method: 'GET',
transformResponse: function(data) {
var response = angular.fromJson(data);
if (response.length) {
// the response is an array, so convert it into an object
var object = {};
for( var i = 0; i < response.length; i ++) {
object[i] = response[i];
}
return object;
} else {
return response;
}
}
},
当我登录时,该服务运行良好,data变量中填充了我需要的json数据。但是,如果超时,data变量将填充一个完整的HTML页面(更具体地说是登录调度页面)。
我不知道我应该如何处理这种情况,并正确地重定向到AngularJS中的那个页面。做到这一点的最佳做法是什么?
感谢
最后,我通过创建一个捕获响应的特殊拦截器来解决问题,如果响应是HTML页面,则执行重定向到登录页面。
resource = $resource(apiEndPointManager.endpoints.masterValues, {}, {
GetByCode: {
url: '{0}/:code'.format(apiEndPointManager.endpoints.masterValues),
method: 'GET',
interceptor: resourceResponseInterceptor
}
拦截器:
angular.module('app.services')
.factory('resourceResponseInterceptor', ['$rootScope', '$q', '$injector', function ($rootScope, $q, $injector) {
var userContext = $injector.get('$userContext');
return {
response: function (response) {
// when session is already timed out, Siteminder will return the login page with an OK code
// so check that response is HTML and if it is redirect user to the Dispatch page
if ($.isHtmlResponseContent(response)) {
window.location.href = userContext.data.LogoutUrl;
return $q.reject(response);
}
return response.resource;
}
};
}]);
$.isHtmlResponseContent = function(response) {
var isHtmlContent = false;
var contentType = response.headers('Content-Type');
if (contentType) {
isHtmlContent = contentType.indexOf('text/html') >= 0;
}
return isHtmlContent;
};
希望它能帮助有同样问题的人!
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 在chrome.tabs.onCreated之后加载HTML页面
- 使用chrome扩展编辑页面html
- 阻止WordPress自动格式化页面HTML
- 如何将数据从一个页面传递到另一个页面html
- 如何使用ceratin Javascript逻辑更新JSP页面html元素
- 我可以通过Javascript从另一个网页获取特定的页面HTML代码吗?
- 淡入 在页面 html 之间淡出并使用数组脚本更改图像
- 我使用XMLHttpRequest作为菜单,当我请求一个包含javascript的页面HTML时,没有任何反应,为什么
- 如何强制浏览器缓存页面.html
- 如何在jQuery中加载页面html和内容(如图像)
- 复制的文件不起作用 - ID在不同页面HTML JavaScript PHP中不起作用
- 如何在没有实际页面刷新的情况下更新页面html和url
- 使用javascript转到另一个页面html
- 如何在页面html区域APEX(Oracle Application Express)上使用共享组件静态文件或CSS文件
- 替换页面html中多次出现的单词
- 如何避免在菜单下拉菜单中重新加载页面?HTML与JQUERY
- 无法重定向其他页面(HTML、Javascript)中的输出
- 提交操作,但显示不同的页面HTML
- 将文本框的值传递给另一个页面html