AJAX request error - ASP. Net and Jquery

Asked

Viewed 604 times

4

I have an aspx file where I created a script with an AJAX call to execute the method populaGridMembros on the table gwMembros, the method is working perfectly and returning a string in JSON format, but when I run the $.ajax does not work, it enters the code no more occurs the Success and it jumps to error. I would like help to solve this problem.

ASPX:

<table id="gwdMembros" class="table table-condensed table-hover table-striped" data-selection="true" data-multi-select="true" data-row-select="true" data-keep-selection="true">
        <thead>
            <tr>
                <th data-column-id="NOME">Nome</th>
                <th data-column-id="SOBRENOME">Sobrenome</th>
                <th data-column-id="RG">RG</th>
                <th data-column-id="CPF">CPF</th>
            </tr>
        </thead>
        <tbody id="tabela">
        </tbody>
    </table>
 </div>

<script src="Scripts/jquery-1.11.1.min.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script src="Scripts/jquery.bootgrid.js"></script>

<script>
    $(function () {
        $.ajax({
            url: "membros.aspx/populaGridMembros",
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: {},
            success: function () {
                populaTabela(data);
            },
            error: function(XMLHttpRequest) {
                alert("Erro");
            }
        });
    });

    function populaTabela(dados) {
        var result = JSON.parse(dados);
        var out;
        for (var it = 0; it < result.length; it++) {
            out += "<tr><td>" + result[it].NOME + "</td></tr>" +
                    "<tr><td>" + result[it].SOBRENOME + "</td></tr>" +
                    "<tr><td>" + result[it].RG + "</td></tr>" +
                    "<tr><td>" + result[it].CPF + "</td></tr>";
        }

        document.getElementById("tabela").innerHTML = out;
    }

    $(function () {
        function init() {
            $("#gwMembros").bootgrid();
        }

        init();

        $("#init").on("click", init);
    });
</script>

In the aspx.Cs of this file I have the following code:

public static string populaGridMembros()
    {
        modelo = new ModelGestaoEclesiasticaDataContext();

        try
        {
            var sourceMembros = from listMembros in modelo.TB_MEMBROs
                                select new
                                {
                                    NOME = listMembros.NOME,
                                    SOBRENOME = listMembros.SOBRENOME,
                                    RG = listMembros.RG,
                                    CPF = listMembros.CPF
                                };

            return JsonConvert.SerializeObject(sourceMembros);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            modelo.Dispose();
        }
    }

    protected void addMembro_Click(object sender, EventArgs e)
    {
        Response.Redirect("Cadastros/cadMembro.aspx");
    }

}
}

1 answer

0

Missed sending of variable date as function setting:

success: function () {
                populaTabela(data);
            },

That way it stays

success: function (data) {
                populaTabela(data);
            },
  • Good morning Jao Assy, thanks for your help. However, even putting the date, which I really forgot to put when I posted the code here, doesn’t work. I can’t see the error.

  • Well, the Success parameter of JQUERY.ajax requires the . status and . readyState of the jqXHR object (passed by Function(data) are respectively 200 and 4. Otherwise it does not execute, however there is another COMPLETE attribute: it accepts any status or readyState. in this case it would be -> complete: Function(data){ //TODO } test if it runs this way, please show the JS errorLog from the browser.

Browser other questions tagged

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