I have tested the Hello world program with Node.js concept and it is working fine.
File details:
index.html
socket.js
run in command prompt : node socket.js
And i have tried the ajax call in node.js using same hello world files and follow the code in the following link,
how to use jQuery ajax calls with node.js
server.js
var http = require('http');
http.createServer(function (req, res) {
console.log('request received');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('_testcb(\'{"message": "Hello world!"}\')');
}).listen(8080);
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: 'http://localhost:8080/',
dataType: "jsonp",
jsonpCallback: "_testcb",
cache: false,
timeout: 5000,
success: function(data) {
$("#test").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
});
</script>
<div id="test"></div>
And i run with the following method in the command prompt
node node.js
And run in the browser like,
Then, It provides the following output
_testcb('{"message": "Hello world!"}')
But i cant get the proper result, Please explain me, how to integrate the ajax in node.js with sample code?
Thanks for your help.