我是 Angular 的新手,下面是从 http url 获取数据并显示的简单代码:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="galaxyCtrlA">
<p>The $http service requests responses from the the server on the "Zone of Avoidance" galaxies, and the response are set as the values of the "message" and "pictures" variables.</p>
<p>Welcome message from the "Zone of Avoidance" galaxies is:</p>
<h1>{{ message }}</h1>
<p>The pictures from the "Zone of Avoidance" are:</p>
<ul>
<br/>
<li ng-repeat="p in pictures">
<img src="{{ p.image }}" alt="{{ p.image }}" width="60%" />
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('galaxyCtrlA', function($scope, $http) {
$http.get("http://www.bogotobogo.com/AngularJS/files/httpRequest/welcome.html")
.then(function(response) {
$scope.message = response.data;
});
$http.get("http://www.bogotobogo.com/AngularJS/files/httpRequest/picture-list.json")
.success(function(response) {
$scope.pictures = response.image;
});
});
</script>
</body>
</html>
上面的输出应该很简单,但我得到一个空白页,没有任何错误。
请指教哪里出了问题
最佳答案
该端点不返回具有 .image
属性的对象,而是返回一个对象数组,每个对象都具有该属性。
[ { image: "/AngularJS/files/httpRequest/Stills_Cam-4_Frame-1300_adjusted.jpg" }, { image: "/AngularJS/files/httpRequest/14-scientistsdi.jpg" } ]
尝试将您的 $scope
对象分配给 response.data
对象属性。另请注意,.success
已弃用,取而代之的是 .then
$http.get("http://www.bogotobogo.com/AngularJS/files/httpRequest/picture-list.json")
.then(function(response) {
$scope.pictures = response.data;
});
关于javascript - AngularJS - 根本不触发/获取 Http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39435538/