error result.lenght ajax with wcf

0

The following script works in part:

<script type="text/javascript">
    function ConsUsuario(){
        var value = $("#codUser").val();

        $.ajax({
        type: "GET",
        url: "ServiceRestPub/ServiceUsuario.svc/ConsultarRegistroPorCodigo/" + value,
        contentType: "application/json",
        dataType: "json",
        success: function (result) {
        alert(result.ConsultarRegistroPorCodigoResult.Nome);
          debugger;
          var tabela = $("#datagrid");
                            var rows = "";
                            tabela.find("tbody td").remove();
                            var jArrayObject = result;
                            for (var i = 0; i < result.length; i++) {
                                var obj = result[i];
                                alert(obj.Login);

                                rows += "<tr>";
                                rows += " <td>" + obj.Codigo + "</td>";
                                rows += " <td>" + obj.Login + "</td>";
                                rows += " <td>" + obj.Nome+ "</td>";
                                rows += " <td> <input type='checkbox' /> </td>";
                                rows += "</tr>";
                            }

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

            //console.info(result.d);
            }
        });
    }
 </script>

When I get to the line for (var i = 0; i

asked by anonymous 11.05.2017 / 12:56

1 answer

0

As your result object is not a list, according to your comment - which should be added to the post, remove the for and continue with the code exactly as it is:

var obj = result.ConsultarRegistroPorCodigoResult
rows += "<tr>";
rows += " <td>" + obj.Codigo + "</td>";
rows += " <td>" + obj.Login + "</td>";
rows += " <td>" + obj.Nome+ "</td>";
rows += " <td> <input type='checkbox' /> </td>";
rows += "</tr>";

If result was a list of objects, it would fit your loop repetition:

$.each(result, function(indice, objeto){
   var obj = objeto.ConsultarRegistroPorCodigoResult
    rows += "<tr>";
    rows += " <td>" + obj.Codigo + "</td>";
    rows += " <td>" + obj.Login + "</td>";
    rows += " <td>" + obj.Nome+ "</td>";
    rows += " <td> <input type='checkbox' /> </td>";
    rows += "</tr>";
}
    
11.05.2017 / 13:03