As a beginner in web dev, I'm building my first web-app using pure node.js. In the registration part, the form data will be posted from the client-side to the server and then added to the database. The client, on success, will manipulate the DOM depending on the response it receives. However, the success callback is always fired before the response is sent.
client-side code:
$.post('/register.db', {
data: JSON.stringify(user),
contentType: "application/json",
dataType: "text",
success: function(data){
alert(data); // data is always undefined
//...manipulate DOM
}
});
server-side code:
var body = "";
req.on("error", function(){
handleError();
}).on("data", function(chunk){
body += chunk;
}).on("end", function(){
// success is fired when request ends rather than response ends
var user = JSON.parse(qs.parse(body).data);
console.log("user ",user);
res.on('error', function(err){
handleError();
});
//...send data to database and write response...
});
Could any kind soul please point out where I did wrong? I would appreciate it a lot!!
via LillyJ
No comments:
Post a Comment