jquery ajax error

Asked

Viewed 64 times

0

I cannot play the result of the ajax query inside a table. The result is an array (result = Object {Query categoriesresult: Array(6)})

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form id="form1">
    <div class="jumbotron">
        <input type="text" id="codUser"/>
        <button onclick="ConsUsuario(); return false;">Consulta Usuario</button>
    </div>

      <div><table id="datagrid"></table></div>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ConsUsuario(){
        $.ajax({
        type: "GET",
        url: "http://food-fast-com.web27.redehost.net/CategoriaService.svc/ConsultarTodosCategorias",
        contentType: "application/json",
        dataType: "json",
        success: function (result) {//result = Object {ConsultarTodosCategoriasResult: Array(6)}
        debugger;

          var tabela = $("#datagrid");
                            var rows = "";
                            tabela.find("tbody td").remove();

          var myData = ConsultarTodosCategoriasResult;
          for (var i = 0; i < myData.length; i++) {
            var obj = myData[i];
            rows += "<tr>";
               rows += " <td>" + result.ConsultarTodosCategoriasResult.Descricao + "</td>";
            rows += "</tr>";
          }

          tabela.html('<tbody>' + rows + '</tbody>');
            }
        });
    }
 </script>

when assigning the result to variable (var myData = Query categoriesresult;) of the following error: Uncaught Referenceerror: Query categoriesresult is not defined

  • Query categoriesresult has not been defined, i.e., it must be in result, from a console.log(result) and see where you find what you need

  • the result is an object array: Object {Query categoriesresult: Array(6)}Query categoriesresult: Array(6)0: Objectcategoria_id: 1Description: "PIZZAS"Filial_id: 1Id: null__proto__: Object1: Object2: Object3: Object4: Object5: Objectcategoria_id: 6Description: "HAMBURGUER"Filial_id: 1Id: null__proto__: Objectlength: 6__proto__: Array(0)proto: Object

  • I got Felipe, changed as follows: var myData = result.Query

  • that’s right haha

1 answer

0


I figured out the problem, I should put result.Query:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form id="form1">
    <div class="jumbotron">
    <input type="text" id="codUser"/>
    <button onclick="ConsUsuario(); return false;">Consulta Usuario</button>
</div>

  <div><table id="datagrid"></table></div>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
function ConsUsuario(){
    $.ajax({
    type: "GET",
    url: "http://food-fast-com.web27.redehost.net/CategoriaService.svc/ConsultarTodosCategorias",
    contentType: "application/json",
    dataType: "json",
    success: function (result) {//result = Object {ConsultarTodosCategoriasResult: Array(6)}
    console.log(result)
    debugger;

      var tabela = $("#datagrid");
                        var rows = "";
                        tabela.find("tbody td").remove();

    var myData = result.ConsultarTodosCategoriasResult
    for (var i = 0; i < myData.length; i++) {
      var obj = myData[i];
      rows += "<tr>";
      rows += " <td>" + obj.Descricao + "</td>";
      rows += "</tr>";
    }

      tabela.html('<tbody>' + rows + '</tbody>');
        }
    });
}

Browser other questions tagged

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