gpt4 book ai didi

javascript - AngularJS - 根本不触发/获取 Http 请求

转载 作者:可可西里 更新时间:2023-11-01 17:32:12 25 4
gpt4 key购买 nike

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

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