Ajax problem while consuming asmx

1

When trying to consume webservice asxm returns unfenined when trying to call the myData.id field, but the result of data.d returns the values of the webservice correctly.

$.ajax({
                type: "POST",
                url: "Service.asmx/SELECT_ALL_CATEGORIAS",
                data: "{ EMP_ID: " + emp + ", UserName: '" + user + "', Password: '" + pass + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    debugger;
                    if (!data) {
                        alert('Fornecedor não encontrado. Por favor verifique e tente novamente.');
                    } else {
                        var tabela = $("#datagrid");
                        var rows = "";
                        tabela.find("tbody td").remove();
                        var myData = data.d;

                        for (var i = 0; i < myData.length; i++) {
                            alert(myData.id);

                            rows += "<tr>";
                            rows += " <td>" + myData.id + "</td>";
                            rows += " <td>" + myData.descricao + "</td>";
                            rows += " <td>" + myData.filial_id + "</td>";
                            rows += " <td> <input type='checkbox' /> </td>";
                            rows += "</tr>";
                        }
                        tabela.find("tbody").html(rows);
                    }
                },
                error: function (data) {
                    debugger;
                    alert('Error');
                }
            });
            return false;
        });
    });

Returning from the webservice by querying in data.d is this below:

  

[{"id":1,"descricao":"PIZZAS","categoria_id":1,"filial_id":1,"categoria1":[],"categoria2":null,"filial":null,"produto_categoria":[]},{"id":2,"descricao":"REFRIGERANTES","categoria_id":2,"filial_id":1,"categoria1":[],"categoria2":null,"filial":null,"produto_categoria":[]},{"id":3,"descricao":"SANDUICHES","categoria_id":3,"filial_id":1,"categoria1":[],"categoria2":null,"filial":null,"produto_categoria":[]},{"id":4,"descricao":"LASANHAS","categoria_id":4,"filial_id":1,"categoria1":[],"categoria2":null,"filial":null,"produto_categoria":[]},{"id":5,"descricao":"SUCOS","categoria_id":5,"filial_id":1,"categoria1":[],"categoria2":null,"filial":null,"produto_categoria":[]}]

webmethod:

[WebMethod]
   // [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SELECT_ALL_CATEGORIAS(int EMP_ID, String UserName, String Password)
    {
        NegLogin login = new NegLogin();
        if (login.UserAuthentication(UserName, Password))
        {
            try
            {
                NegCategoria negCategoria = new NegCategoria();
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                string json = string.Empty;
                json = jsSerializer.Serialize(negCategoria.SELECT_ALL_CATEGORIAS(EMP_ID));
                return json;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        else
        {
            return "Sem acesso ao conteúdo, verifique o usuário e senha!";
        }
    }
}
    
asked by anonymous 09.04.2017 / 05:53

1 answer

0

For example, this myData is an array, so within that for loop you have to access the element you want to iterate, that is, using its index.

Edit

I have seen your comment:

  

If you put alert(typeof myData) , the word String

That means you do not have a json yet. You have to use JSON.parse .

Example:

var myData = JSON.parse(data.d);
for (var i = 0; i < myData.length; i++) {
  var obj = myData[i];
  alert(obj.id);

  rows += "<tr>";
  rows += " <td>" + obj.id + "</td>";
  rows += " <td>" + obj.descricao + "</td>";
  rows += " <td>" + obj.filial_id + "</td>";
  rows += " <td> <input type='checkbox' /> </td>";
  rows += "</tr>";
}
    
09.04.2017 / 05:57