1

I am new to Angular and below is the simple code which should fetch data from the http urls and display :

    <!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>

The output for the above should be pretty simple but i get a blank page , no errors at all .

Kindly advice what's going wrong

scniro
  • 16,844
  • 8
  • 62
  • 106
user1411837
  • 1,494
  • 6
  • 27
  • 41

3 Answers3

1

That endpoint does not return an object with an .image property, but is instead an array of objects each with that property.

[ { image: "/AngularJS/files/httpRequest/Stills_Cam-4_Frame-1300_adjusted.jpg" }, { image: "/AngularJS/files/httpRequest/14-scientistsdi.jpg" } ]

Try instead assigning your $scope object to the response.data object property. Also note that .success is deprecated in favor of .then

$http.get("http://www.bogotobogo.com/AngularJS/files/httpRequest/picture-list.json")
    .then(function(response) {
       $scope.pictures = response.data;
    });
scniro
  • 16,844
  • 8
  • 62
  • 106
0

The problem is with the second request. It should be :

$http.get("http://www.bogotobogo.com/AngularJS/files/httpRequest/picture-list.json")
    .success(function(response) {
       $scope.pictures = response;
    });
Zakaria
  • 14,892
  • 22
  • 84
  • 125
0

It is not parsing the json in my test. Does this work?

$http.get("picture-list.json").success(function(response) {

    var data = JSON.parse(response);
    $scope.pictures = data.image;
});

Here is a json string that i know is working: "{\"image\":[{\"image\":\"url.jpg\"}]}"

Darin Cardin
  • 627
  • 1
  • 4
  • 10