我是棱角分明的新手。这里我有代码:我收到数字的响应数据。在此代码中,我如何将响应数据分配为$ scope.vote_counting。在此代码中不返回任何内容。
$scope.votes = function(){
var votes = $http({
method: "post",
url: "/getVotes",
data: { id: $scope.Id}
}).success(function(response){
});
return votes;
}
请有人帮忙。
答案 0 :(得分:3)
只需致电$http
即可。它不一定是函数
$http({
method: "post",
url: "/getVotes",
data: { id: $scope.Id }
}).then(function(response) {
//handle success
$scope.votes_counting = response.data;
}, function(error){
//handle error
});
排序版本是
$http.post("/getVotes", { id: $scope.Id }).then(function(response) {
//handle success
$scope.votes_counting = response.data;
}, function(error) {
//handle error
})
注意:您正在使用POST方法,但GET方法似乎更适合您的情况(getVotes
)
答案 1 :(得分:1)
我添加了一个片段,其中显示了promises的基本处理方式。在这里,我使用了一个服务来模拟一个http调用。响应附加到范围变量,该变量显示在视图中。
angular.module('TestApp', [])
.factory('MockHttp', function($q) {
return {
getMockData: function() {
return $q.when(['A', 'B', 'C']);
}
};
})
.controller('TestController', function($scope, MockHttp) {
$scope.res = null;
MockHttp.getMockData()
.then(function(res) {
$scope.res = res;
})
.catch(function(err) {
console.log(err);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="TestApp">
<div ng-controller="TestController">
{{res}}
</div>
</div>
答案 2 :(得分:0)
$ http函数不返回服务器的响应。但是,正如您已经发现的那样,您可以使用success函数来获取服务器响应。只需在成功函数中设置$scope.votes
值,如下所示:
$http({
method: "post",
url: "/getVotes",
data: { id: $scope.Id}
}).success(function(response){
$scope.votes = response
});
答案 3 :(得分:0)
最简单的可能是使用$http.post。请注意,public void GetData() {
try{
SoapObject request = new SoapObject("InFLAirBookService",
"InflAirGDSLCCAvail");// second parameter is your method name which you want to call
request.addProperty("AccountID", value1);//value1 contains value of AccountID
request.addProperty("AccountPassword", value2);//value2 contains value of AccountPassword
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new
HttpTransportSE("airwebservice.ezeeibe.com/InflAirBook.asmx");
androidHttpTransport.call("InFLAirBookService/InflAirGDSLCCAvail",
envelope);
SoapPrimitive objs = (SoapPrimitive)
envelope.getResponse();//objs will have the response from webservice in string
//if SoapPrimitive does not work then write SoapObject.
} catch (Exception e) {
e.printStackTrace();
}
}
}
已弃用,而不是success
:
then
另请注意,$scope.retrieveVotes = function(){
$http.post('/getVotes', {id : $scope.id}).then(function(response){
$scope.votes = response.data;
});
}
调用是异步的,因此调用$http
也是异步的。