Problem sending data to the controller from jquery ajax

0

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?

    
asked by Carlos 10.12.2018 в 11:54
source

1 answer

0

I put in response the comment that Orlando has made me.

Leave the contenType as: application/json; charset=utf-8
and the parameter data pass the parameter like this: JSON.stringify({ id: param })

    
answered by 10.12.2018 / 15:42
source