Ajax problem while consuming asmx

Asked

Viewed 43 times

1

When trying to consume asxm webservice it returns unfenined when trying to call the myData.id field, however the date result. d returns the webservice values 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;
        });
    });

return of the referring webservice on the date. d is the one 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!";
        }
    }
}
  • 1

    What gives console.log(typeof myData);?

  • stands alert(typeof myData), the word String appears in a loop while passing character by character

  • In that case you have to convert to json, I edited my answer.

1 answer

0


For the example you gave myData is an array thus within this cycle for you must access the element you want to iterate, that is using its index.

Edit

I just saw your comment:

stands alert(typeof myData), appears the word String

That means you don’t 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>";
}

Browser other questions tagged

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