jquery - Sending json data using Ajax POST methods with Node Js? -


i trying send data html table json file , pass server node, on server replicated more once when pass json can read it, since passes data server run more twice, not know if there method run 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');                 });         });     }); }); 

i'm not sure router works... never seem 1 router .json extension.

if i'm wrong, please let me know :)

but... have not serialized data in request, javascript object until serialize json. try put datatype , content type data.

       $.ajax({         type: "post",         url: "http://localhost:8080/enviando_datos.json",         datatype: "json",         contenttype: 'application/json', //see         data: listjson        }).done(function ( data ) {           alert("ajax callback response: "+json.stringify(data));       }); 

call json.stringify in order serialize json , body parser has parse.


Comments