0

I am a beginner in angular and I want to retrieve the http get response of an url (http://myurl.com) to make after some manipulations.

I test that but no result :/

var app = angular.module('plunker', []);

app.factory('myService', function($http) {
  return {
    async: function() {
     return $http.get('http://apibuilder-apidiscovery.kermit.rd.francetelecom.fr/infrastructures/16857');
    }
  };
});

app.controller('MainCtrl', function( myService,$scope) {
  myService.async().then(function(d) { //2. so you can use .then()
    $scope.data = d;
  });
});

HTML

<body>
    <div ng-controller="MainCtrl">
        Data: {{data}}<br>
    </div>
</body>

Thank you so much.

1

2 Answers 2

1

The data assignment should be $scope.data = d.data;

Controller

app.controller('MainCtrl', function( myService,$scope) {
  myService.async().then(function(d) { //2. so you can use .then()
    $scope.data = d.data;
  }, function(error){
     console.log('Error occured');
  });
});

Note also make sure that if you are trying to make call to external domain, the CORS should be enabled. Additionally you might need to pass autherization headers in the request.

Sign up to request clarification or add additional context in comments.

Comments

0

script.js

var app = angular.module('plunker', []);

app.factory('myService', function($http,$q) {

    var myService = {
        async : async,
    };
    return myService;

    function async() {
        var d = $q.defer();
        $http({
            method: 'GET',
            url: 'http://apibuilder-apidiscovery.kermit.rd.francetelecom.fr/infrastructures/16857',
            headers: {
                "Content-Type": "text/plain",
                // you can add headers here
            }
        }).then(function(reply) {
            d.resolve(reply.data);
        }, function(error) {
            d.reject(error.data);
        });
        return d.promise;
    }

});

app.controller('MainCtrl', function(myService, $scope) {

  myService.async()
    .then(function(result) {
        $scope.data = result;
  });

});

and the index file,

<!DOCTYPE html>
<html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-app="plunker" ng-controller="MainCtrl">
    Data: {{data}}<br>
  </body>

</html>

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.