Question: Ajax access object

Asked

Viewed 31 times

-1

Good evening, folks. Could you help me with a question.

I have set up an ajax so that I can update the table asynchronously, but I cannot access the ajax return Object. my code:

    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
 $.ajax({
  dataType: "json",
  type: 'GET',
  url: "/api/vendedor",
}).done(function( data ) {
    var atualizaTable = '';
    $.each(data, function(key, value) {
      console.log(value);
     atualizaTable += '<tr>';
     atualizaTable += '<td>'+ value.nome +'</td>';
     atualizaTable += '<td>'+ value.rg +'</td>';
     atualizaTable += '<td>'+ value.email +'</td>';
     atualizaTable += '<td>'+ value.totalVendas +'</td>';
     atualizaTable += '</tr>'
     });
     $('#atualizaTable').append(atualizaTable);
  });
});

My question is to access the data that is within the function $.each(data, function(key, value) it’s returning me this way in console.log(value)

[{…}]
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5ebdf58ef56ac5794a5bb047", nome: "Victor", rg: 19278080, email: "[email protected]", totalVendas: 10, …}
1: {_id: "5ec3295b944349133c2100c7", nome: "JULIANO", rg: 10152020, email: "[email protected]", totalVendas: 5, …}
2: {_id: "5ec32975944349133c2100c8", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
3: {_id: "5ec3297a944349133c2100c9", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
4: {_id: "5ec3299d944349133c2100ca", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}
5: {_id: "5ec32a76944349133c2100cb", nome: "Juliano", rg: 1010, email: "[email protected]", totalVendas: 2, …}

What I need to do to access this data if I step one value.nome he returns me undefined I keep waiting, obigado.

  • There’s an index [0] there. Try value[0].nome.

  • Simm, there is. But I have more than one name in this indece if I pass value[0]. name it accesses a guy only

  • And if I access as quoted it returns me: Uncaught Typeerror: Cannot read Property 'name' of Undefined

  • As it is returning the data accessing only the Indice value[0]: {_id: "5ebdf58ef56ac5794a5bb047", name: "Victor", rg: 19278080, email: "[email protected]", totalVendas: 10, ...}

  • returns Undefined =(

2 answers

0

For those who have the same question I spent a $.each to go through again

  $(document).ready(function () {
        $.ajax({
          type: 'GET',
          url: "/api/vendedor",
          dataType: "json",
        }).done(function (data) {
          var atualizaTable = '';
          $.each(data, function (key, value) {
            $.each(value, function (key, value) {
              atualizaTable += '<tr>';
              atualizaTable += '<td>' + value.nome + '</td>';
              atualizaTable += '<td>' + value.rg + '</td>';
              atualizaTable += '<td>' + value.email + '</td>';
              atualizaTable += '<td>' + value.totalVendas + '</td>';
              atualizaTable += '</tr>'
            });
            $('#atualizaTable').append(atualizaTable);
          });
        });
      });

0

It seems to me that the date has an index [0] with arrays, so you have to do the . each in the return index [0], and not at the root in the array:

$.each(data[0], function(key, value) {
           ↑↑↑

Browser other questions tagged

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