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

标签 javascript html angularjs http

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

相关文章:

javascript - vendor.js 中的 SCRIPT1002 语法错误 - Angular 8 IE11

html - body 里面有头标?

javascript - 当我只想 onclick 时显示页面加载时的警报

javascript - 多个视频的播放/暂停功能

javascript - AngularJS 变量未在 html 中显示

javascript - 在 JavaScript 中检查二叉树是否对称

javascript - 动态编辑元素

javascript - 无法使用Angular.js隐藏表格行的按钮

JavaScript 无法将 input.min、input.max、input.value 识别为整数

angularjs - 使用 Angularjs 显示带有时区的日期和时间