【发布时间】:2015-08-07 13:49:11
【问题描述】:
我正在尝试创建一个显示由http://rest-service.guides.spring.io/greeting 检索到的数据的基本网页。
json输出为:
{"id":2273,"content":"Hello, World!"}
我正在使用以下 html 页面:
<body ng-app="hello">
<div class="container">
<h1>Greeting</h1>
<div ng-controller="home" ng-cloak class="ng-cloak">
<p>The Id is: {{greeting.id}}</p>
<p>The content is: {{greeting.content}}</p>
</div>
</div>
<script src="js/angular-bootstrap.js" type="text/javascript"></script>
<script src="js/hello.js"></script>
</body>
还有 hello.js:
var myApp = angular.module('hello', []);
myApp.controller('home', ['$scope', function($scope) {
$scope.greeting = {};
$http.get('http://rest-service.guides.spring.io/greeting')
.success(function(data, status, headers, config) {
$scope.greeting = data;
});
}]);
结果:占位符 greeting.id/content 未解析。这里可能有什么问题?
【问题讨论】:
-
如果在 http 块上方,您写:
$scope.greeting = {"id": "test", "content": "test2"};您可以检查 $http 请求是否有问题 - 是吗? -
@brendan 是的,在这种情况下一切正常。
-
好的。那么如果不是
$scope.greeting = data;,而是$http 中的console.log(data);呢?抱歉,如果您已经完成了所有这些操作,但它可能会更好地为我们阅读而隔离问题 -
在执行 get 请求时收到
Access-Control-Allow-Origin header missing。
标签: javascript json angularjs rest