I have an aspx file where I created a script with an AJAX call to execute method populaGridMembros
on table gwMembros
, method is working perfectly and returning a string in JSON format, when I run $.ajax
does not work, it goes into the code but does not and it jumps to the error. I would like help solving 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");
}
}
}