Problem passing information from AJAX to Controller

0

I have the following ajax that takes the order code. At the moment I can get the order code, however, it sends 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);
                        }
                    }
                });

            }

Code:

publicList<Models.PedidoVendaItens>ListarPedidos(stringcodigo){try{Models.PedidoVendaItensobjItens=newModels.PedidoVendaItens(config){ItPNumero=codigo};returnnewModels.PedidoVendaItens(config).ListarItensPedido(codigo);}catch(Exceptionex){throwex;}}

    
asked by anonymous 23.03.2018 / 20:09

1 answer

2

To send a primitive type per post, you should do a little 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"
    
23.03.2018 / 20:35