I'm working on a cordova application, using html5 and javascript.
The architecture is the following one : the phone application requests something to the server, which asks a firebird database. The database answers to the server which gives the asked data to the phone application (in html5 / javascript).
I've been able to send the data from the server to the phone with JSON, and I thought it would be the same to send some data from the phone app to the server. However, I have no idea how to send data to such a server from the phone. I've tried to simplify the problem as much as possible.
So considering the following javascript code :
var send = { "name":"John", "age":30, "car":null };
var sendString = JSON.stringify(send);
alert(sendString);
xhttp.send(sendString);
(the alert does send me : {"name":"John","age":30,"car":null} )
How do I retrieve it in my Node JS server ? For the moment, my code is the following one :
app.post('/createEmp', function(req, res){
//res.send(req.body.name);
//console.log('test :' + req.app.post('name'));
//console.log(req);
console.log('createEmp');
if(typeof(req) == 'undefined') {console.log('Y a rien'); }
else {
console.log('La req n est pas vide, son type est : ' + typeof req);
}
console.log(typeof req.query.name);
});
I let the comments so that you know what I already tried (there are more)... Each time, the type of req is either defined or it is an object, but since it is circular I can't parse it so I'm not sure it is the data sent from the phone application.
So could you please give me a piece of advice, an explication about how I could send the data from the phone to the server ? (I guess I could try to show the data in a url that would be parsed by the server but I prefer not having to do so to protect the data...).
Any help would be appreciated ! Thank you very much !
(PS : I've already been looking for some answers everywhere but nothing worked yet)
req
is an object full of stuff that comes with every request. You need to get body of your request. This might help you: How to retrieve POST query parameters?
But because there's not much client-side JavaScript i ust ask: Have you specified you want to POST this?
Try do it like here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send ...and when you use xhr.setRequestHeader("Content-Type", "application/json") you probably don't need to stringify this.