Im making a simple app to learn how node.js works. Using jquery and ajax i pass a JSON object to a node.js method. The problem is that i don't know how to get the specific JSON data in the node.js method.
client-side code:
$(document).ready(function(){
$('#guardarVehiculo').click(function(){
var car = JSON.stringify(agregarCarro());
$.ajax({
type: 'POST',
data: car,
url: '/saveCar',
dataType: 'JSON'
}).done(function( response ) {
console.log(response);
});
});
});
function addCar(){
var id = getById('id').value;
var brand = getById('brand').value;
var model = getById('model').value;
var transmission = getById('automatic').checked ? getById('automatico').value : getById('mechanic').value;
var comment = getById('comment').value;
var car = new Car(id, brand, model, transmission, comment);
return car;
}
node.js code:
var express = require('express');
var fs = require('fs');
var app = express();
app.get('/', function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post('/saveCar', function(req, res){
console.log(req);
});
var server = app.listen(8000, function(){
var host = server.address().address;
var port = server.address().port;
console.log('Server running at: ' + host + ':' + port);
});
Thanks in advance.
req.query