【发布时间】:2015-11-12 12:33:03
【问题描述】:
<body ng-app="myApp">
<div ng-controller="customersCtrl">
<table>
<tr ng-repeat="res in customers">
<td>{{ $index + 1 }}</td>
<td>{{ res.Name }}</td>
<td>{{ res.City }}</td>
</tr>
</table>
</div>
</body>
var myApp = angular.module('myApp', []);
myApp.controller("customersCtrl", function ($scope,$http) {
$http.post('Default.aspx/Getdata', { data: {} })
.success(function (response) {
$scope.customers = response.d;
})
.error(function (data, status, headers, config) {
alert(status+"-------"+data);
});
});
服务器端功能:
[WebMethod]
public static string Getdata()
{
List<customors> people = new List<customors>{
new customors{Name = "biss",City="Lebanon"},
new customors{Name = "jiji",City="America"}
};
JavaScriptSerializer serialiser = new JavaScriptSerializer();
string json = serialiser.Serialize(people);
return json; // [{"Name":"biss","City":"Lebanon"},{"Name":"jiji","City":"America"}]
}
public class customors
{
public string Name{get;set;}
public string City{get;set;}
}
但是没有显示结果。我做错了什么?
【问题讨论】:
-
在视图和控制器中打错
customors和customers -
这不是我想的问题,因为如您所见:
.success(function (response) {$scope.customers = response.d;}) -
返回的数据是否正确?换句话说……如果你中断了成功回调……你有正确的数据吗?
-
要添加到上面,你不应该也使用HTTP get而不是post吗?
-
是的
responce.d返回[{"Name":"biss","City":"Lebanon"},{"Name":"jiji","City":"America"}]
标签: javascript c# angularjs