我正在尝试将带有WSSE
身份验证的Ajax调用转换为AngularJS工厂。
该方法为Post
。
此用途的目的是访问Adobe Analytics Rest API并返回要转换为JSON的数据,然后使用d3.js进行可视化。
我不熟悉AngularJS $http post
调用中可以使用的属性,因此不确定执行WSSE auth
,dataType
,{{1}的正确方法是什么等等
这是来自公共github repo的原始ajax代码:
callback
这是代码与纯JS一起使用的当前方式:
(function($) {
window.MarketingCloud = {
env: {},
wsse: new Wsse(),
/** Make the api request */
/* callback should follow standard jQuery request format:
* function callback(data)
*/
makeRequest: function (username, secret, method, params, endpoint, callback)
{
var headers = MarketingCloud.wsse.generateAuth(username, secret);
var url = 'https://'+endpoint+'/admin/1.4/rest/?method='+method;
$.ajax(url, {
type:'POST',
data: params,
complete: callback,
dataType: "text",
headers: {
'X-WSSE': headers['X-WSSE']
}
});
}
};
})(jQuery);
我想分别将它转换为工厂和控制器。
到目前为止,这是我为工厂所做的事情:
MarketingCloud.makeRequest(username, secret, method, params, endpoint, function(response) {
data = JSON.parse(response.responseText);
});
这就是我对控制器的看法:
app.factory('mainFactory', ['$http', function($http) {
var wsse = new Wsse ();
return function(username, secret, method, params, endpoint) {
return $http({
method: 'POST',
url: 'https://' + endpoint + '/admin/1.4/rest/?method=' + method,
data: params,
headers: {
'X-WSSE': wsse.generateAuth(username, secret)['X-WSSE']
},
dataType: 'text',
});
};
}]);
目前我收到错误app.controller('mainController', ['$scope', 'mainFactory', function($scope, mainFactory) {
mainFactory.success(function(data) {
$scope.data = data;
});
}]);
,我认为这是因为工厂还没有工作。
答案 0 :(得分:0)
我自己已经解决了这个问题。我传递给工厂中第一个函数的参数已经全局定义,因此被覆盖了。 无论如何都不需要第一个功能。
这是工厂代码:
app.factory('mainFactory', ['$http', function($http) {
var wsse = new Wsse ();
return {
getAnalytics : function (){
$http({
method: 'POST',
url: 'https://' + endpoint + '/admin/1.4/rest/?method=' + method,
data: params,
headers: {
'X-WSSE': wsse.generateAuth(username, secret)['X-WSSE']
}
})
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}
};
}]);
这是控制器代码:
app.controller('mainController', ['$scope', 'mainFactory', function($scope, mainFactory) {
$scope.title = "Inn Site";
$scope.data = mainFactory.getAnalytics();
}]);