How do I send an AJAX request on a different port with jQuery?

ajax, cross-domain, javascript, jquery

Solution

You cannot `POST` information cross domain, subdomain, or port number. You can however use JSONP if you have access to both the daemon and the requesting site. If data needs to be returned, then the `daemon` needs to support a `callback` query parameter and return it properly formatted.

Pass the information to the daemon:

$.getJSON('http://domain.com:8080/url/here?callback=?', {
  key: 'value',
  otherKey: 'otherValue'
}, function(data){
     // Handles the callback when the data returns
});

Now just make sure your daemon handles the `callback` parameter. For instance, if `callback=mycallback` the return from the daemon (the only thing written to the page) should look like this:

For an key/value pairs:

mycallback( {'returnkey':'returnvalue', 'other':'data' });

For an array:

mycallback( [1,2,3] );

If you do not have a JSONP or similar mechanism in place, you cannot communicate cross domain using jQuery.

Problem

I need to send an AJAX request to, for example, port 8080 where a daemon is running there.

Original source