我剛開始學習Angular.js。如何在Angular.js中重寫以下代碼?
var postData = "<RequestInfo> "
+ "<Event>GetPersons</Event> "
+ "</RequestInfo>";
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4 || req.readyState == "complete") {
if (req.status == 200) {
console.log(req.responseText);
}
}
};
try {
req.open('POST', 'http://samedomain.com/GetPersons', false);
req.send(postData);
}
catch (e) {
console.log(e);
}
這裏是我迄今爲止 -
function TestController($scope) {
$scope.persons = $http({
url: 'http://samedomain.com/GetPersons',
method: "POST",
data: postData,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
$scope.data = data; // how do pass this to $scope.persons?
}).error(function (data, status, headers, config) {
$scope.status = status;
});
}
HTML
<div ng-controller="TestController">
<li ng-repeat="person in persons">{{person.name}}</li>
</div>
上午我在正確的方向?
非常感謝。它工作完美。 – tempid 2013-04-11 14:32:59
謝謝,我已經嘗試了很多的解決方案,但標題:'{'Content-Type':'application/x-www-form-urlencoded'}'竅門 – 2014-06-01 22:19:25
請注意,'success'和'error'函數有現已棄用。 – Ali 2015-12-07 12:57:23