Function with jQuery recording in two tables does not persist in BD

Asked

Viewed 143 times

2

My need is as follows: write to a table, take the generated ID and write to the second table with this ID. As the order is to do this in jQuery, a function has been made (by a colleague). But unfortunately I cannot save.

See that there are two calls in AJAX. If I remove the second step (persist in the second table), the code works. But when I enable the second code, then nothing works, neither the first nor the second calls. Below my code jQuery. If I was going to do it in Code Behind, I would know how to do it, because I’ve done a lot, but with jQuery I’m not getting it.

$(function () {
    $("#btnGravarPassageiros").click(function () {
        var result = {
            Id: 0,
            Nome: $("input[name ='txtNome']").val(),
            CPF: $("input[name='txtCep']").val(),
            Email: $("input[name='txtEmail']").val(),
            DataNascimento: $("input[name='txtAno']").val() + "-" + $("input[name='txtMes']").val() + "-" + $("input[name='txtDia']").val(),
            Telefone: $("input[name='txtTelefone']").val(),
            Celular: $("input[name='txtCelular']").val(),
            Endereco: $("input[name='txtLogradouro']").val(),
            Numero: $("input[name='txtNumero']").val(),
            CEP: $("input[name='txtCep']").val(),
            Complmento: $("input[name='txtComplemento']").val()
        };

        var result =  [];
        var resultado;
        var cont = 0;

        $.ajax({
            url: '/Passo/addCliente',
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: "POST",
            data: JSON.stringify({ _tb_clientes: result }),
            success: function (data) {
                alert(data.Result);
                $(".passageiro").each(function () {
                    nm = "txtNomePassageiro" + cont;
                    dia = "txtDiaPassageiro" + cont;
                    mes = "txtMesPassagecontro" + cont;
                    ano = "txtAnoPassagecontro" + cont;
                    sexo = "txtSexo" + cont;
                    numpassaporte = "txtPassaporte" + cont;
                    diavalidade = "txtDiaVal" + cont;
                    mesvalidade = "txtMesVal" + cont;
                    anovalidade = "txtAnoVal" + cont;
                    paisemissao = "txtPaisEmissao" + cont;
                    resultado = jQuery.parseJSON(
                        '{"Id_Cliente" : "' + data.Result +'" , "Nome": "' + $("input[name =" + nm + "]").val() +
                        '", "PassaPorte": "' + $("input[name =" + numpassaporte + "]").val() +
                        '", "DataNascimento": "' + $("input[name =" + dia + "-" + mes + "-" + ano + "]").val() + 
                        '", "Sexo": "' + $("input[name =" + sexo + "]").val() + '", "PassaPorteValidade": "' +
                        $("input[name =" + diavalidade + "-" + mesvalidade + "-" + anovalidade + "]").val() +
                        '", "PassaPortePais": "' + $("input[name =" + paisemissao + "]").val() + '" }');
                    result.push(resultado);

                    cont++;
                });

                $.ajax({
                    url: '/Passo/addClientePassageiro',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    data: JSON.stringify({ _tb_clientes: result }),
                    success: function (data) {},
                    error: function (error) {}
                });                           
            },
            error: function (error) {}
        });
    });
});

People realized the following, that the recording function in my controller, works, at least in the first phase(1st table), but in BD is all null, as if recognizing the parameters passed from CSHTML. This is occurring.

2 answers

1

There seems to be no error in the Ajax calls, at least nothing that draws attention and indicates a wrong construction.

Note that the code that uses jQuery in your example only executes posts. It is quite likely that the error happens on the server side. I realized that the error handling functions are empty:

error: function (error) {}

These functions receive three parameters (see documentation). I suggest you change the error functions to the following form:

error: function (xhr, status, message) {
    // Aqui dentro você depura
}

This will allow you to see what is the real error that occurs. If it is on the server side, once you say experienced with . NET, I believe it will be something easy for you to solve ;)

-2

The mistake was here:

var result =  []; 

The variable result had already been declared. I changed to:

var dados [];

Now you’re recording.

Browser other questions tagged

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