Requests POST AJAX ASP net MVC

Asked

Viewed 488 times

1

Good afternoon, I’m with a small doubt, I’m making a POST request with AJAX on ASP net MVC and it works normal, but in firefox I get an error on the console inserir a descrição da imagem aqui

However, this error does not appear on Chrome. Here is the requisition code:

    function adicionaCarrinho(quantidade, codigoProduto) {

    var token = $('input[name=__RequestVerificationToken]').val();
    var header = {};
    header['RequestVerificationToken'] = token;

    $.ajax({
        url: "/Produto/AddCarrinho",
        type: 'POST',
        data: { __RequestVerificationToken: token, codigoProduto: codigoProduto, quantidade: quantidade},
        headers: header,
        success: function (data) {
            getCookie();
        },
        error: function (data) {

            console.log('Error' + data);
        }
    });
}

and here’s the controller code

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddCarrinho(string codigoProduto, int quantidade)
        {
            HttpCookie cart = Request.Cookies.Get("Cart");
            HttpCookie cartQtd = Request.Cookies.Get("CartQtd");

            if(cart != null)
            {
                _pedidoItensDAO.InsereItem(_pedidoDAO.RetornaPedido(cart.Value), codigoProduto, quantidade);
                cartQtd.Value = (Convert.ToInt32(cartQtd.Value) + quantidade).ToString();
                Response.Cookies.Add(cartQtd);
            }
            else
            {
                var token = Guid.NewGuid().ToString();
                _pedidoDAO.InserePedido(token);
                _pedidoItensDAO.InsereItem(_pedidoDAO.RetornaPedido(token), codigoProduto, quantidade);
                Response.Cookies.Add(new HttpCookie("Cart", token));
                Response.Cookies.Add(new HttpCookie("CartQtd", quantidade.ToString()));
            }

            return new EmptyResult();
        }

someone knows the reason for the firefox error?

  • I’m not seeing anything wrong, it’s more a rsrs kick. For testing... try to create a class and inside add the properties codigoProduto and quantidade, it may be that he is waiting for the data via QueryString as it is.

1 answer

1


Based on some research I found that:

The error message is generated only by Firefox when the rendering is empty. For some reason, .NET generates a type of response of application/xml when creating an empty tab. Firefox analyzes the file as XML and finds no root element, send the error message. Source: here

In your case you’re returning a EmptyResult(); which returns a completely empty result. To solve the problem return an object Json emptiness return Json(string.Empty)

  • 1

    It worked, exactly this, I had looked for something but had found nothing, all I found was referring to ajax request with some problem, I must have searched wrong xD worth !!

Browser other questions tagged

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