I try to send a parameter to the controller from jquery using an ajax call and by POST. I get an error 500.
Failed to load resource: the server responded to a status of 500 (Internal Server Error)
My controller's code is this:
[Authorize]
public class MiController: Controller
{
...
[HttpPost]
public ActionResult GetProvincias(int? id)
{
...
return Json(_miServicio.GetProvincias(id));
}
}
And the jquery code is this:
function CargarProvincias(param) {
let ddlProvincias = $('#dll_Provincia');
ddlProvincias.empty();
ddlProvincias.append('<option selected="true" disabled>Seleccionar provincia</option>');
ddlProvincias.prop('selectedIndex', 0);
$.ajax({
url: window._getProvinciasUrl,
data: { id: param },
type: 'POST',
contentType: "application/json; charset=utf-8",
success: function (respuesta) {
$.each(respuesta, function (index, datos) {
ddlProvincias.append($('<option></option>').attr('value', datos.IdProv).text(datos.NomProvincia));
});
},
});
}
In the different tests that I have done I have observed that:
- If I delete the parameter of the controller action, it works correctly.
- If instead of using POST, I use GET, it also works correctly.
For more examples that I look at, they all go like this.
What escapes me?