Bakke Medina
Bakke Medina

Reputation: 307

Sending json data using Ajax POST methods with Node Js?

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');
                });
        });
    });
});

Upvotes: 0

Views: 1314

Answers (1)

Sayuri Mizuguchi
Sayuri Mizuguchi

Reputation: 5338

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.

Upvotes: 1

Related Questions