在AngularJS中使用Wcf数据服务

时间:2015-07-17 10:33:32

标签: javascript c# json angularjs rest

我是AngularJS的新手。我正在尝试使用AngularJS来使用Wcf数据服务。我一直都在失败,因为我不确定哪里出了问题。有人可以帮我这个。感谢。

如果查询,数据服务将返回Json:

http://localhost/Wcf/DataService/Report/ReportService.svc/SystemCategories?$format=json

示例Json返回:

{"odata.metadata":"http://localhost/Wcf/DataService/Report/ReportService.svc/$metadata#SystemCategories","value":[
    {"ID":1,"SystemName":"System-A","Description":"System A"},
    {"ID":2,"SystemName":"System-B","Description":"System B"},
    {"ID":3,"SystemName":"System-C","Description":"System C"}]}

代码(来自w3school的样本)

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="systemCat"> 
<ul>
  <li ng-repeat="x in categories">
    {{ x.ID + ', ' + x.SystemName }}
  </li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('systemCat', function($scope, $http) {
  $http.get("http://localhost/Wcf/DataService/Report/ReportService.svc/SystemCategories?$format=json")
  .success(function (response) {$scope.categories = response.value;});
});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这段代码应该可行,如果不看一下javascript控制台,你发现的任何错误都可以让你知道出了什么问题。

<script>
  var app = angular.module('myApp', []);
  app.controller('systemCat', function($scope, $http) {
    $http.get('http://localhost/Wcf/DataService/Report/ReportService.svc/SystemCategories?$format=json')
      .success(function (data) {
        console.log(data);
        $scope.categories = data.value;
      })
      .error(function (data) {
        console.log('error!');
      });
  });
</script>