【问题标题】:Problems with AJAX and node.js interactionAJAX 和 node.js 交互的问题
【发布时间】:2012-10-25 00:06:12
【问题描述】:

我是 node.js 的新手,我正在尝试将数据从 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>

内容在&lt;div&gt; 中,但没有显示,也没有提醒任何东西。在安慰浏览器时出现此错误。

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

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

【问题讨论】:

    标签: ajax node.js


    【解决方案1】:

    您似乎正试图在您的域之外发出请求。看看这个答案:https://stackoverflow.com/a/9327231/1417431

    您可以在此处进一步阅读:Same origin policy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2013-05-20
      相关资源
      最近更新 更多