I have set up a simple server in Node.js. I am trying to get the post data from the request. I have tried sending the data with $.post() and by a simple HTML form with method="post"
if(request.method.toUpperCase() === "POST") {
var $data;
request.on("data", function (chunk) {$data += chunk});
request.on("end", function ()
console.log($data);
});
var message = "Settings Saved"
response.writeHead(
"200",
"OK",
{
"access-control-allow-origin" : origin,
"content-type" : "text/plain",
"content-length" : message.length
}
);
return(response.end(message));
}
Node returns the Status:200 and everything looks good, but no matter where I put it, the console.log($data) always spits out undefined (BTW I have also tried this without the $ under a different variable name).
Looking at all the other SO questions about this that I could find did not have a solution for me.
use var $data = '';
not var $data;
var $data; // $data === undefined.
//...
$data += chunk // undefined + 'string' === 'undefined'