0

Is possible to send ajax request from apache server (e.g : http://localhost/myscript) to node js without any problems ??

I try that and it's work perfectly but it's not working in mozilla only IE.

my ajax :

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

my nodejs server :

var express = require("express");

var app = express.createServer();

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

app.listen(3000);

is possible to use this in my projects without problemes?

nodejs
  • 1
  • 1

1 Answers1

1

It's not possible to using normal XHR (aka Ajax) in that case. See: Can I use XMLHttpRequest on a different port from a script file loaded from that port?

You'll have to either use JSONP (which allows cross-domain data retrieval)

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

See: http://api.jquery.com/jQuery.ajax/

Other to set up a proxy on your main domain to do the conversion between the two ports. They are some projects doing that already:

Community
  • 1
  • 1
greut
  • 4,305
  • 1
  • 30
  • 49
  • You dont need JSONP, you can just do CORS manually by setting the headers correctly – Raynos Jan 15 '12 at 13:20
  • 1
    [CORS](http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/) aren't available on every browsers are they? http://caniuse.com/#feat=cors – greut Jan 15 '12 at 13:22
  • IE6/7 and opera fail. You will need to do feature detection or shimming for IE8/9 – Raynos Jan 15 '12 at 13:30