gpt4 book ai didi

javascript - 在 AngularJS 中使用 Wcf 数据服务

转载 作者:行者123 更新时间:2023-11-30 00:25:50 25 4
gpt4 key购买 nike

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

关于javascript - 在 AngularJS 中使用 Wcf 数据服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com