I'm doing a query to an api with asp.net, the query is well done and everything but I have a problem filling a table with ajax, I could not see the data you send me
here the code c #
public async Task<List<Sto_TraerCaracterizacionRespuestas>> Buscar(string contexto)
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync("http://localhost:53931/api/TraerCaracterizacionRespuestas?cod="+contexto+"&mod=nose");
var lista = JsonConvert.DeserializeObject<List<Sto_TraerCaracterizacionRespuestas>>(json);
return lista;
}
here the ajax code
<script>
function buscar() {
var valorEscogido = $("#Sto_TipoAlertas_Preguntas_TipoAlerta_Pregunta_CodigoPRegunta1").val();
@**$.post('@Url.Action("Buscar")/?contexto=' + valorEscogido);*@
$.ajax({
url: "@Url.Action("Buscar")",
data: "contexto=" + valorEscogido,
type: "POST",
contentType: "application/x-www-form-urlencoded",
dataType: "json",
success: function (data) {
$("#contenido").html('');
if (data != null && $.isArray(data)) {
$.each(data, function (index, value) {
$("#contenido").append("<tr><td>" + value.CodigoInterno1 + "</td><td>" + value.Respuesta + "</td><td>" + value.Detalles+"</td></tr>");
});
}
}
});
}
the html of the view
<h2>Index</h2>
<div class="row form-group">
<div class="col-md-10">@Html.DropDownListFor(x => x.Sto_TipoAlertas_Preguntas.TipoAlerta_Pregunta.CodigoPRegunta1, Model.Sto_TipoAlertas_Preguntas.ListarComboBoxAPI, "Seleccionar una", new { @class = "form-control" })</div>
</div>
<button onclick="buscar()">Buscar</button>
<table class="table table-bordered" id="contenido">
<thead>
<tr>
<th>CodigoInterno</th>
<th>Detalles</th>
<th>Respuesta</th>
</tr>
</thead>
<tbody></tbody>
</table>
Well, I'm new to Ajax and I do not know what I'll be doing wrong.