ajax和nodejs

时间:2012-01-15 11:46:37

标签: ajax node.js

是否可以从apache服务器(例如:http://localhost/myscript)向节点js发送ajax请求而没有任何问题?

我尝试了它并且它的工作完美但它不仅仅在Mozilla IE中工作。

我的ajax:

$.ajax({
url : "http://localhost:3000/test_ajax",
type: "GET",
success : function(data){
    alert(data);
}
});

我的nodejs服务器:

var express = require("express");

var app = express.createServer();

app.get('/test_ajax', function(req, res){
res.send('Hello World');
});

app.listen(3000);

可以在没有问题的项目中使用它吗?

1 个答案:

答案 0 :(得分:1)

在这种情况下,不可能使用普通的XHR(也就是Ajax)。请参阅:Can I use XMLHttpRequest on a different port from a script file loaded from that port?

您必须使用JSONP(允许跨域数据检索)

$.ajax({…, dataType: 'jsonp'});

请参阅:http://api.jquery.com/jQuery.ajax/

其他在主域上设置代理以在两个端口之间进行转换。他们已经做了一些项目: