我有一个带有 http.post 请求的 Angular Controller ,但没有得到响应,该服务工作正常,因为使用 postman 对其进行了测试。
当页面加载时,我需要在 HTLM 表中显示来自 WS 的数据(我知道如何执行此部分),我正在发送变量名“data”中的请求正文、变量名“config”中的 header 配置以及对我的 WS 的 URL 的 http.post 调用。
我是 Angular 新手,所以我不知道我是否遗漏了一些东西,我也想在控制台中打印响应以测试它是否返回我所期望的内容。
我从网上找到的一个示例中获取了这段代码,并对其进行了修改,在该示例中,有一个按钮可以调用 SendData() 函数,但我不需要按钮,调用必须如下进行页面已加载。
这是 Controller 代码
.controller("HttpGetTestController", function ($scope, $http) {
$scope.SendData = function () {
var data = {
"userId":"mdrodri",
"token":"840430482834947828876768058086529370654",
"domain":"MX",
"filters":{
"timeFrameType":"Week (FiscalYear)",
"custMembSelectionType":"TOTAL",
"locationSelectionType":"Country",
"merchandiseSelectionType":"SBU",
"startYear":"2015",
"endYear":"2015",
"startUnit":"1",
"endUnit":"1",
"comparator":false,
"locationSelections":["CA"],
"merchandiseSelections":["SBU38"],
"custMembSelections":["TOTAL"],
"metricSelections":["Product Sales (Category Rank)"],
"rankOrder":"10"
},
"additionalFilters":[],
"cache":false
};
var config = {
headers : {
'Content-Type': 'application/json;'
}
}
$http.post('http://whateverurl/', data, config)
.success(function (data, status, headers, config) {
$scope.PostDataResponse = data;
console.log("Success");
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
console.log("Data: " + data);
console.log("Status: " + status);
console.log("Headers: " + header);
console.log("Config: " + config);
});
};
})
在我的 HTML 中,这就是我调用该 Controller 的方式
<div class="panel panel-default" ng-controller="HttpGetTestController">
感谢您的时间和帮助。
最佳答案
您需要在 Controller 中调用 $scope.SendData() 函数:
.controller("HttpGetTestController", function ($scope, $http) {
$scope.SendData = function () {
//your code ...
}
//then the calling
$scope.SendData();//here
}
关于javascript - 从 Angular 柱方法获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34537043/