У меня есть следующий код для вызова службы на клиентской стороне бизнес-приложения, которое я разрабатываю (с.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;
}
}
},
Когда я вошел в систему, эта служба работает хорошо, и переменная данных заполняется данными json, которые мне нужны. Однако, если у меня есть тайм-аут, переменная данных заполняется всей 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;
};
Надеюсь, это поможет кому-то с той же проблемой!