【发布时间】:2014-10-22 19:04:53
【问题描述】:
我正在为 AngularJS 创建一个 SOAP 请求拦截器
看起来像这样:
angular.module('myApp')
.factory('SoapInterceptor', ['$q', function ($q) {
var soapRequest = function (url, SOAPAction, requestEnvelope, callback) {
$.soap({
url: url,
appendMethodToURL: false,
SOAPAction: SOAPAction,
enableLogging: false,
data: requestEnvelope,
success: function (SOAPResponse) { callback(SOAPResponse.toJSON()); },
error: function (SOAPResponse) { throw new Error(SOAPResponse); }
});
}
return {
'request': function (config) {
if (config.data && config.data.isSoap) {
var deferred = $q.defer();
soapRequest(config.url, config.data.soapaction, config.data.requestEnvelope, function (data) {
angular.extend(data, config);
deferred.resolve(data);
});
return deferred.promise;
}
return config;
},
'response': function (response) {
// I somehow want this returned response to be my soap response
// which i have got in request but of course it's no use there
return response;
}
}
}]);
所以我可以像这样在数据存储的方法中使用它:
var deferred = $q.defer();
$http.post("http://myapi.com/service.asmx",
{
isSoap: true,
requestEnvelope: reqXml,
soapaction: "http://myapi.com/CampaignsGetList"
})
.success(function (data) {
deferred.resolve(data);
});
return deferred.promise;
当 isSoap 为真时,请求正确地将其传递给我的 soapRequest,但我如何才能将我返回的响应传递给响应函数返回,以便我的消费者可以愉快地使用该承诺?
感谢任何帮助。
【问题讨论】:
-
您是否尝试使用拦截器自己处理 $http 调用?您能否看看我的答案,让我知道这里是否有我遗漏的东西?谢谢!
标签: javascript angularjs soap