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

7
  • Is this also the domain you're using? Also looking at the files, looks like response.image would be empty Commented Sep 11, 2016 at 11:37
  • No this isnt a domain i am using . Its a different domain . Commented Sep 11, 2016 at 11:39
  • Before marking this as duplicate kindly understand what i am asking and if the output are the same rather than trying to act too smart . @Pankaj Parkar Commented Sep 11, 2016 at 12:04
  • @user1411837 apologies, do you have collcetion inside response.image? Commented Sep 11, 2016 at 12:09
  • CORS Need to be enabled from the server side and The second http request is returning you just the image name in text i guess. Commented Sep 11, 2016 at 12:24

3 Answers 3

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;
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks But sorry that didnt make any difference
0

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;
    });

Comments

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\"}]}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.