在AngularJS中使用Wcf数据服务

Consuming Wcf Data service in AngularJS

本文关键字:Wcf 数据 服务 AngularJS      更新时间:2023-09-26

我是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>

这段代码应该可以工作,如果没有的话,看看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>