각도 $ http 인터셉터를 사용하여 ajax 요청이 401 (인증되지 않음)을 반환하는지 확인합니다. 응답이 401이면 원래 요청이 대기 상태가되고 로그인 양식이 표시되며 로그인에 성공하면 대기중인 요청을 다시 시도합니다. 이것은 이미 $의 http로 작동하고, 각 인터셉터의 소스는 다음과 같습니다
define('common.service.security.interceptor', ['angular'], function() {
'use strict';
angular.module('common.service.security.interceptor', ['common.service.security.retryQueue'])
.factory('securityInterceptor', [
'$injector',
'$location',
'securityRetryQueue',
function($injector, $location, securityRetryQueue) {
return function(promise) {
var $http = $injector.get('$http');
// catch the erroneous requests
return promise.then(null, function(originalResponse){
if(originalResponse.status === 401){
promise = securityRetryQueue.pushRetryFn('Unauthorized', function retryRequest(){
return $injector.get('$http')(originalResponse.config);
});
}
return promise;
});
};
}
])
// register the interceptor to the angular http service. method)
.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');
}]);});
가 어떻게 바람 요청이 각 $에 http 인터셉터를 사용하여 만들 수 있습니까?
Breeze는 "Breeze/Adapters/breeze.ajax.angular.js"파일에서 각 $ http 서비스에 대한 래퍼를 제공합니다.
angular.js 디버깅breeze.config.initializeAdapterInstance("ajax", "angular", true);
는, 지금 사실 바람이 $ HTTP를 사용하는 것을 알 수 있지만, 위의 등록 된 인터셉터를 실행하지 않습니다 그래서 첫 번째 아이디어는 그것을 사용하는 바람을 이야기했다. $ http 안에는 등록 된 인터셉터를 저장하는 배열 "reversedInterceptors"가 있습니다. 이 배열을 콘솔에 기록합니다. $ http를 사용하면이 배열의 길이는 (예상대로) 1이지만 breeze로 요청할 때이 배열은 비어 있습니다.
질문 : 어떻게이 $ http 인터셉터를 breeze 요청과 함께 사용할 수 있습니까? 여기
날 바람 각도 아약스 어댑터와 함께 HTTP 인터셉터를 사용하기 위해 setHttp 방법이 작동하여 바람
define('breeze.ajax.angular.module', ['breeze', 'angular'], function (breeze) {
'use strict';
/* jshint ignore:start */
var core = breeze.core;
var httpService;
var rootScope;
var ctor = function() {
this.name = "angular";
this.defaultSettings = {};
};
ctor.prototype.initialize = function() {
var ng = core.requireLib("angular");
if (ng) {
var $injector = ng.injector(['ng']);
$injector.invoke(['$http', '$rootScope',
function (xHttp, xRootScope) {
httpService = xHttp;
rootScope = xRootScope;
}]);
}
};
ctor.prototype.setHttp = function (http) {
httpService = http;
rootScope = null; // to suppress rootScope.digest
};
ctor.prototype.ajax = function (config) {
if (!httpService) {
throw new Error("Unable to locate angular for ajax adapter");
}
var ngConfig = {
method: config.type,
url: config.url,
dataType: config.dataType,
contentType: config.contentType,
crossDomain: config.crossDomain
}
if (config.params) {
// Hack: because of the way that Angular handles writing parameters out to the url.
// so this approach takes over the url param writing completely.
// See: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
var delim = (ngConfig.url.indexOf("?") >= 0) ? "&" : "?";
ngConfig.url = ngConfig.url + delim + encodeParams(config.params);
}
if (config.data) {
ngConfig.data = config.data;
}
if (!core.isEmpty(this.defaultSettings)) {
var compositeConfig = core.extend({}, this.defaultSettings);
ngConfig = core.extend(compositeConfig, ngConfig);
}
httpService(ngConfig).success(function (data, status, headers, xconfig) {
// HACK: because $http returns a server side null as a string containing "null" - this is WRONG.
if (data === "null") data = null;
var httpResponse = {
data: data,
status: status,
getHeaders: headers,
config: config
};
config.success(httpResponse);
}).error(function (data, status, headers, xconfig) {
var httpResponse = {
data: data,
status: status,
getHeaders: headers,
config: config
};
config.error(httpResponse);
});
rootScope && rootScope.$digest();
};
function encodeParams(obj) {
var query = '';
var key, subValue, innerObj;
for (var name in obj) {
var value = obj[name];
if (value instanceof Array) {
for (var i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += encodeParams(innerObj) + '&';
}
} else if (value instanceof Object) {
for (var subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += encodeParams(innerObj) + '&';
}
} else if (value !== undefined) {
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
}
breeze.config.registerAdapter("ajax", ctor);
breeze.config.initializeAdapterInstance("ajax", "angular", true);
/* jshint ignore:end */
});
빙고! @ almaplayer. 'setHttp' FTW. 아래 내 대답을 참조하십시오. – Ward