ASP NET MVC with JQuery

1

I made an ajax request, but it does not call the controller, when debugging javascript never drops in success and the console appears Error processing XML.

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Cadastro(Pedido pedido)
{
  if (ModelState.IsValid)
  {
    var appPedido = new PedidoAplicacao();
    appPedido.Salvar(pedido);
    return Json(new { Resultado = "Sucesso" });
   }
 return Json(new { Resultado = pedido }, JsonRequestBehavior.AllowGet);
 }

View where I call the js function:

        <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
                    <a href="#" id="SalvarPedido">Enviar</a>
        </div>
    </div>

Script

 function SalvarPedido() {


    var valor =  $('#Valor').val();
    var cadastro =  $('#Data').val();

    var url = "/Pedido/Cadastro";


    $.ajax({

        url: '/Pedidos/Cadastro' ,
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: { Id: 0, Data: cadastro, Valor: valor },            
        success: function (data) {
                alert('Ok');  
        }

    })

He does not even go to the controller. The following is the console image:

Errorcodegeneratedintheaboveimage:

  

ErrorprocessingXML:noelementfoundLocation: link

    
asked by anonymous 30.03.2017 / 19:48

1 answer

2

You need to serialize your JavaScript object to JSON:

function SalvarPedido() {    

    var valor =  $('#Valor').val();
    var cadastro =  $('#Data').val();   

    var data = { Id: 0, Data: cadastro, Valor: valor };    
    var dadoSerializado = JSON.stringify(data);

     $.ajax({

          url: '/Pedidos/Cadastro',
          type: "POST",
          dataType: "json",
          contentType: 'application/json; charset=utf-8',
          data: dadoSerializado,            
          success: function (data) {
                        alert('Ok');  
          }
     })
}
    
30.03.2017 / 22:51