Function with jQuery writing to two tables does not persist in BD

2

My need is this: I write to a table, get the generated ID and write to the second table with that ID. As the order is to do this in jQuery, a function has been made (by a colleague). But unfortunately I can not write.

Notice 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, nothing works, neither the 1st nor the 2nd calls. Below my jQuery code. If it were to do in Code Behind, I would know how to do it, because I already did a lot, but with jQuery I can not do 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) {}
        });
    });
});

Personally I realized the following, that the write function in my controller works, at least in the first phase (1st table), but in the BD it gets everything null , as if it were recognizing the past parameters of CSHTML. This is happening.

    
asked by anonymous 17.03.2014 / 12:48

2 answers

1

There seems to be no error in Ajax calls, at least nothing that catches your eye and indicates a wrong build.

Note that the code that uses jQuery in your example just runs posts. It is quite likely that the error will happen on the server side. I noticed that the error handling functions are empty:

error: function (error) {}

These functions are given 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 the real error is. If it is on the server side, once you say you are experienced with .NET, I believe it will be something easy to solve;)

    
17.03.2014 / 13:10
-2

The error was here:

var result =  []; 

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

var dados [];

Now you are recording.

    
17.03.2014 / 15:02