2012-11-05 30 views
1

我是新来的node.js,我想从AJAX将数据发送到服务器的Node.js并取回了things.My服务器端代码问题与AJAX和Node.js的互动

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); 

而且即在客户端运行的是

<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> 

内容是在<div>但是,这并不表明了它也不警报anything.On安慰浏览器我得到这个错误。

XMLHttpRequest cannot load http://localhost:8124/test?page=ProfilePage. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

这是一种错误的做事方式吗?请帮忙。

回答