How to read POST data using node.js without using express or connection
Edit
Make sure you don't match request and response objects. everything will be easier if you don't.
I am trying to pass POST data from a form with two inputs to a specific url node. I originally used the javascript xmlHTTPrequest object to send it. Data was received, but node could never read the object I was trying to send, which was JSON format. This is how it looked when I converted it to a string:
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket:
{ _handle:
{ writeQueueSize: 0,
socket: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
errorEmitted: false,
bytesRead: 8442,
bytesWritten: 1669569,
allowHalfOpen: true,
writable: true,
readable: true,
This is not a complete line. I switched to using an ajax post request instead because I thought serializearray () could create a different format that the node could read, but it was the same. My server-side code is essentially simple:
function email(request, response)
form = '';
request.setEncoding("utf8");
request.on('data', function(chunk) {
console.log('data received');
form += chunk.toString();
});
request.on('end', function() {
console.log('done receiving post data');
});
My form looks like
<form name="email" action="javascript:true;">
<input type="text" name="address" value="Enter your email address" onFocus="this.value=''">
<input class="special" type="text" name="honeypot">
<input type="submit" value="submit">
</form>
If I try to parse the JSON.parse an error is thrown by an unexpected o If I try to parse with a request it returns an empty object
If I try something like console.log (form.address) the output is undefined but should be one from the values presented in the form.
my http server will be routed to / email and this is the function that responds to / email.
I checked and I know that the on 'data' event has not expired yet because my code is set up to notify me of the 'data'event and I can see the event is happening. Locally, this code works fine and I can read the data presented, but on development server it just doesn't work.
I know people will say that I should use connect or express, but let's just assume that isn't an option, how can I get node to handle this data correctly? The jQuery that represents the form is more or less standard. Many thanks!
EDIT:
Sorry, I thought the issue would be fixed as I noticed the posted string was not JSON. Here is the code I am using to send the message.
$(document).ready(function() {
$("#email").submit(function(event) {
event.preventDefault();
var data = $(this).serializeArray();
$.ajax({
type: 'POST',
dataType: 'json',
url: '/email',
data: JSON.stringify(data),
success: function(msg) {
$("#aviso").html(msg);
}
});
});
});
Here is the line that stores the data variable:
[
{
"name": "address",
"value": "foo@bar.com"
},
{
"name": "honeypot",
"value": "irobot"
}
]
This is valid JSON according to jsonlint. But still the same error when reaching the server. node handles the "data" event and all is well, until I try to parse the string it throws an undefined error: 1 unexpected o.