I am new to node.js and I am trying sending data from AJAX to Node.js server and getting back the things.My server side code is
var http = require('http');
var url=require('url');
http.createServer(function (req, res) {
console.log(req.url);
var url_parts = url.parse(req.url, true);
switch(url_parts.pathname)
{
case '/':
res.writeHead(302,{'location':'http://localhost/testajax/index.html'});
res.end();
break;
case '/test':
console.log('request received');
res.writeHead(200, {'Content-Type': 'text/html'});
console.log(url_parts.query["page"]);
res.end(url_parts.query["page"]);
break;
}
}).listen(8124);
And that running on client side is
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function(){
$.get('http://localhost:8124/test', {'page':$(this).text()}, function(data){
alert(data);
$('#content').html(data);
});
});
});
</script>
The content is in the <div>
but that doesnt show up neither does it alert anything.On consoling the browser I get this error.
XMLHttpRequest cannot load http://localhost:8124/test?page=ProfilePage. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
Is this a wrong way of doing things?Please help.