Problem passing information from AJAX to Controller

Asked

Viewed 42 times

0

I have the following ajax that picks up the order code. So far I can pick up the order code, but sends as null to controller

var btn = document.getElementById("btnNumeroPed");
            btn.onclick = function () {
                var dataJson = JSON.stringify({ "PedNumero": $("#inputNumero").val() });
                //var dataJson = JSON.stringify({ "clientes": data });
                $.ajax({
                    type: "POST",
                    url: "/Loja/ListarPedidos?PedNumero=" + $("#inputNumero").val(),
                    data: dataJson,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        if (data > 0) {
                            $("#txtCodigoProduto").val(data.itPNumero);
                            $("#txtDescProduto").val(data.itPDescrProd);
                            $("#txtQtdeProduto").val(data.itPQtde);
                            $("#txtPontos").val(data.itPPontos);
                            $("#txtPreco").val(data.itPrecoComp);
                            $("#txtDesconto").val(data.itPDesconto);
                            $("#txtTotal").val(data.itPTotal);
                        }
                    }
                });

            }

inserir a descrição da imagem aqui

Code:

public List<Models.PedidoVendaItens> ListarPedidos(string codigo)
        {
            try
            {
                Models.PedidoVendaItens objItens = new Models.PedidoVendaItens(config)
                {
                    ItPNumero = codigo
                };
                return new Models.PedidoVendaItens(config).ListarItensPedido(codigo);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

inserir a descrição da imagem aqui

1 answer

2


To send a primitive type per post, you must perform a small gambiarra (which is even documented).

1st Option - send a JSON with the empty property.:

curl -X POST "http://localhost:xpto/api/Loja/ListarPedidos" -H "accept: application/json" -H "Content-Type: application/json" -d '{ "": "00000027" }'

2nd Option - add a = before the value to be sent.:

curl -X POST "http://localhost:xpto/api/Loja/ListarPedidos" -H "accept: application/json" -H "Content-Type: application/json" -d "=00000027"

Or modify the signature of your method to receive this data by Querystring

public List<Models.PedidoVendaItens> ListarPedidos([FromQuery] codigo)

Then make the following call

curl -X POST "http://localhost:xpto/api/Loja/ListarPedidos?codigo=00000027" -H "accept: application/json"

Browser other questions tagged

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