GET request with Angularjs

Asked

Viewed 350 times

1

Good afternoon I am trying to perform a Get request with Angularjs but I get no response and the only console.log that runs is the last one that is already outside the request;

        angular.module("listaTelefonica").controller("listaTelefonicaCtrl", function ($scope, $http) {
        $scope.contatos = [];
       var carregarContatos = function(){
          $http({method:'GET',url: 'http://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1'}).then(function(response){
            console.log("conexão foi realizada") 
            console.log(response.data);

          }).catch(
             console.log("falha na conexão")
          );
          console.log("falha na conexão")
       };
       console.log("falha na conexão")

2 answers

0


Try it this way:

var url = 'http://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1';
var carregarContatos = function(){
      return $http.get(url).then(function(response){
        console.log("conexão foi realizada") 
        console.log(response.data);

      }).catch(
         console.log("falha na conexão")
      );
      console.log("falha na conexão")
   };
   console.log("falha na conexão")

0

If you have passed your code through some transpilator, such as Babel, or minificador, you should always use the services declared in the creation of the controller.

and Voce forgot, that first comes the name of the controller, and then comes the service array. lacked the "[" "]"

angular.module("listaTelefonica").controller("listaTelefonicaCtrl", ["$scope", "$http", function ($scope, $http) {

//Seu codigo aqui

}]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.