0

I am trying to send data from an html table with a json file and pass it to the server node, but on the server it is replicated more than once when I pass the json it can read it, since it passes all the data to the server but it run more than twice, I do not know if there will be a method to only run it once.

Client Side:

function enviarDatos(listJson){
    $.ajax({
    type: "POST",
    url: "http://localhost:8080/enviando_datos.json",
    data: listJson,
    success: function(data) {
        //show content
        alert('Success!');
    },
    error: function(jqXHR, textStatus, err) {
        //show error message
        //alert('text status '+textStatus+', err '+err);
    }
  });
}

Server Side:

app.post('/enviando_datos.json', function(req, res){
    //sacar eprocentaje de consumo
    //sacar valor de fondo fijo
    var fecha_actual = new Date();
  var dd_actual = fecha_actual.getDate();
  var mm_actual = fecha_actual.getMonth()+1; //hoy es 0!
  var yyyy_actual = fecha_actual.getFullYear();
  fecha_actual = yyyy_actual+"-"+ mm_actual+'-'+dd_actual;
    var estado = 0;
    if(req.body.tipo == 'VALE DE PAGO'){
        var valor_base = req.body.valor;
    }else{
        var valor_base = req.body.valor - (req.body.valor*0.14);
    }
    db_handler.obtener_fondo_categoria(req.body.categoria,function(queryResMontoMax){
        db_handler.insertar_proveedores_ruc_cedula(req.body.proveedor,req.body.ruccedula,function(queryRes1){
            db_handler.insertar_datos_caja_chica_con_factura(
                req.carPoolSession.username,
                fecha_actual,
                req.body.valortotal,
                queryResMontoMax[0].MONTO_MAX,
                '15%',
                req.body.empresa,
                req.body.categoria,
                req.body.proveedor,
                req.body.ruccedula,
                req.body.entregado,
                req.body.cargado,
                req.body.fecha,
                req.body.valor,
                req.body.tipo,
                req.body.estabfact,
                req.body.ptoemifact,
                req.body.numsecfact,
                req.body.numautofact,
                '14%',
                valor_base,
                req.body.estabret,
                req.body.numemiret,
                req.body.numsecret,
                req.body.numautoret,
                estado,
                function(queryRes2){
                    console.log('datos ingresados con exito');
                });
        });
    });
});
1
  • on client how are you calling the method enviarDatos? can you share your client side js? Commented Apr 5, 2017 at 20:03

1 Answer 1

1

I'm not sure about this router will works... I never seem one router with .json extension.

If I'm wrong, please let me know :)

But... You have not serialized the data in your request, now is just a JavaScript object until you serialize it as JSON. Try put the dataType and content type about your data.

       $.ajax({
        type: "POST",
        url: "http://localhost:8080/enviando_datos.json",
        dataType: "json",
        contentType: 'application/json', //see that
        data: listJson
       }).done(function ( data ) {
          alert("ajax callback response: "+JSON.stringify(data));
      });

Call JSON.stringify in order to serialize as JSON and then the body parser has something to parse.

Sign up to request clarification or add additional context in comments.

2 Comments

With your I get this error, SyntaxError: Unexpected token f, I do not know what token is, on the client side comes this error: 400 (Bad Request).
Verify your json bdfore send the data, if all okay, try add JSON.stringify(listJson)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.