Vue-Resource $http.get does not recognize json with [

Asked

Viewed 399 times

0

I have a php that delivers the following content

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

Vue-Resource only recognizes if I remove the []

my code is

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

In PHP I’m doing so:

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

I’m forgetting something?

This same json, I can read through Angular.

  • Something you can also do is exchange your PHP code for print json_encode($smtm->fetchAll());

  • And to debug codes so you can use console.log(response) and see on your console how the data is coming.

  • Just want to show a record of that JSON? Wouldn’t that be something like: http://jsfiddle.net/a312sa5a/ ?

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;
}
  • It worked!!! now I want to play it all in a v-for would only be Sponse.body;?

  • this.breweries = Sponse.body

  • I tried it here and it worked, all right now. Thanks Friends

0

The ideal is to avoid writing Promises this way, not only from the margin to miss keys but also has a bad reading.

Try it like this next time:

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

Note that you have the return calling directly the key of your API. If you need to work the return before doing something just use keys in Arrow Function.

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

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

    })

Browser other questions tagged

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