I am just starting to play around with Node.js. I have a simple test setup.
var sys = require('util'),
http = require('http'),
qs = require('querystring');
http.createServer(function (req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data
});
req.on('end', function () {
var POST = qs.parse(body);
console.log(POST);
});
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('success');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
My test client JS
$.post("http://127.0.0.1:1337", "This is my POST data.", function (data) {
alert(data);
});
$.get("http://127.0.0.1:1337", function (data) {
alert(data);
});
If I browse to the address where my HTTP server is running : http://127.0.0.1:1337 I see the response text, "success".
However when I use $.post or $.get they both succeed with a status of 200 but no response. The $.get and $.post callback functions are never called, and I do not see an alert appear. What am I doing wrong?
Thanks!