Return response problem via $http.post()

Asked

Viewed 510 times

-2

I’m just trying to get a return value HTTP but I’m not getting it.

.controller('myCtrl', function ($scope, $http) {
 $scope.sendPost = function() {
    var dataObj = {
            name : $scope.newName
    };  
    var res = $http.post('xxxx.consulta.php', dataObj)

    res.success(function(data, status, headers, config) {
        $scope.hello = data;
    });  
 }
})

When I run the page in the browser it returns the object to me JSON:

[{"id":"13","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"}]

Would you like to save only id and name, how do I do? I have tried data.id or in the view {{hello.id}} and it didn’t work...

  • Try this: $scope.hello = data.data

  • Didn’t work...

  • use console.log(data) inside the Success, which it shows on your console?

  • [Object] 0: Object departamento: "EAD" id: "13" img: "https://image.freepik.com/icones-gratis/perfil-macho-utilizador-sombra_318-40244.png" nome: "Yuri" proto: Object length: 1 proto: Array[0]

  • 1

    And if you do so: = data[0] ?

  • It worked like this, but I believe that is not so...pq will have several array in a query, so I will only take one...

  • to do so You would need to loop the array, I will add response

Show 2 more comments

2 answers

3


Your problem, as stated in the comments, occurs because you are returning an array, but you want to use only one object (from what I understand). For this you need to make a filter, or loop. See these examples:

angular.forEach(data, function(obj){
    if(sua logica aqui) {
        return obj; //retorne ou aplique o valor ao seu objeto.
    }
})

So you’re interacting all over the array, going through each item and doing a check.

Or you can make a filter, see:

$scope.valor = $filter('filter')(data,{id: $scope.seuvalor});

The two methods will filter your array and assign only one array value. To use these methods, let’s assume that you want to take the object that has the same name as yours $scope.newName, then you would do so:

angular.forEach(data, function(obj){
    if(obj.name == $scope.newName) {
        return obj; //retorne ou aplique o valor ao seu objeto.
        //ou
        $scope.novoValor = obj; //Atribui todo o objeto ao $scope 
    }
})

//Ou então

$scope.novoValor = $filter('filter')(data,{name: $scope.newName});

2

The concept is very simple, this is a JSON object, so you have to play it in a variable and use it as in the example below, you can either query by position, or in a loop, by your controller, you can play in the $scope.collection = data:

"Collection" would be the same as a date type variable, as below:

var data = [
  {"id":"13","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"},

  {"id":"14","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"}
]

//exemplo 1
console.log(data[0].id)
console.log(data[0].nome)

//exemplo 2
console.log(data[1].id)
console.log(data[1].nome)

//no laço:
for (var i in data) {
  console.log(data[i].nome)
  console.log(data[i].id)
}

In the view, you can use ng-repeat:

<div ng-repeat="dado in collection">
   {{ dado.id }} <br>
   {{ dado.nome }}
</div>

Or pick up by the position in the view:

<div>
       {{ collection[0].id }} <br>
       {{ collection[0].nome }}<hr>

       {{ collection[1].id }} <br>
       {{ collection[1].nome }}
</div>

Browser other questions tagged

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