vue vue-resource $ http.get does not recognize json with [

0

I have a php that delivers the following content

[{"Id":"50630","Operador":"","Id_cadastro":"61693"}]

The vue-resource only recognizes if I remove the []

My code is vue

this.$http.get('data.json').then(function(response) {
     this.cervejarias = response.data.Id_cadastro;
}

In PHP I'm doing this:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $data[] = $row;
}
print json_encode($data);

Am I forgetting something?

This same json, I can read through the Angular.

    
asked by anonymous 26.12.2016 / 14:48

2 answers

2

The correct property of response is body and not data .

// supondo que o retorno seja [{"Id":"50630","Operador":"","Id_cadastro":"61693"}]
this.$http.get('data.json').then(function(response) {
  this.cervejarias = response.body[0].Id_cadastro;
}
    
26.12.2016 / 15:56
0

The ideal is to avoid writing promises in this way, not only the margin to miss keys but also a poor reading.

Try this at the next:

this.$http.get('data.json')
    .then(res => res.json())
    .then(res => this.cervejarias = res.Id_cadastro)

Notice that you have the return by calling the key directly from your API. If you need to work the return before doing something just use keys in the arrow function.

this.$http.get('data.json')
    .then(res => res.json())
    .then(res => {

        // múltiplas linhas
        this.cervejarias = res.Id_cadastro)

    })
    
19.07.2017 / 16:08