1

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.

log N
  • 925
  • 9
  • 33

1 Answers1

0

It seems that you are trying to make a request outside your domain. Have a look at this answer: https://stackoverflow.com/a/9327231/1417431

You could have a further read here: Same origin policy

Community
  • 1
  • 1
mike
  • 880
  • 1
  • 8
  • 12