ASP NET MVC with Jquery

Asked

Viewed 279 times

1

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

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');  
        }

    })

It does not go to the controller. Follow the console image: inserir a descrição da imagem aqui

Error code generated in the image above:

Error in XML processing: no element found Position: http://localhost:52305/ddc764bf50734262b0fe681dedc10612/browserLinkSignalR/abort? transport=webSockets&connectionToken=Aqaaancmnd8bfderjhoawe%

  • What is exactly the error? And where does it occur? In the browser console or Visual Studio?

  • In the browser console

  • And the first question?

  • I put the error print in the description.

  • Copy the text from the print and paste it in text format into the question, the images here in the company are blocked, I can’t see.

  • XML parsing error: no element found Position: http://localhost:52305/ddc764bf50734262b0fe681dedc10612/browserLinkSignalR/abort? transport=webSockets&connectionToken=Aqaaancmnd8bfderjhoawe%

Show 1 more comment

1 answer

2

You need to serialize your Javascript object for 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');  
          }
     })
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.