im sending a simple ajax-jqurey post request :
$('#postButton').click (function () {
var empInfo = $('#empForm').serialize();
var empData = JSON.stringify(empInfo,null,2);
$.ajax({
type: 'POST',
dataType: 'JSON',
url: 'http://localhost:3000/post',
data: empData, // check how to stringfy to json and send() .
success: function(serverData){
$('.thankU').html(serverData);
}
});
i have tried every way i know to get the data into json format on the node server :
app.post('/post', function(req, res){
// get query params as object
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
console.log(JSON.parse(chunk) );
console.log(qs.parse(chunk) );
console.log(JSON.stringify(chunk));
});
});
and the result is :
Received body data:
"bname=jjj&burl=jfjfj.com&bEmail=ff&bPhone=0608584884&fieldSelected=webPro"
bname=jjj&burl=jfjfj.com&bEmail=ff&bPhone=0608584884&fieldSelected=webPro
instead of thw way it appers in developer tools\network : "bname:jjj burl:jfjfj.com bEmail:ff bPhone:0608584884 fieldSelected:webPro"
Try to change
dataType: 'JSON',
to:
dataType: 'json',
the jquery serialize method
creates an encoded string out of the form: http://api.jquery.com/serialize/
to get a json object, try to use serializeArray
http://api.jquery.com/serializeArray/ instead.
Or create your json object manually and read each form elements' $().val()
value...
Your other code looks fine, you have to stringify the object and post it to the server.