result.lenght ajax error with wcf

Asked

Viewed 22 times

1

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 on the line go (var i = 0; i < result.length; i++) { to pick up the amount of characters to scan the message object is: Local i : 0 obj : Undefined result : Object and the process stops! How do I scan the object?

  • I am new to javascript, but I believe the result is: {"Consultarregistroporcodigoresult":{"Code":2,"Code":1,"Login":"ednilson1","Name":"Ednilson","Registration":true,"Password":"123456","Type":"D"}} which is the return of the http query://localhost/ServiceRestPub/ServiceUsuario.svc/ConsultarRegistroPorCodigo/2

  • And what property that obj you want to work on?

  • i wanted to read the fields Code, Login and name the play them on the table to be displayed

  • In this context you don’t even need the is, if result is a simple object.

  • By @Aline, lack of attention from me, thank you very much worked ok, I do not know if it should be asked here or should be another post, but if the table had more records, how would this be, Lenght work?

  • I added the answer. =)

Show 1 more comment

1 answer

0


Since your result object is not a list, according to your comment - it 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 were a list of objects, it would fit your repetition loop:

$.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>";
}
  • Thanks so much @Aline for the help, solved my problem!

Browser other questions tagged

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