By user198729

2010-01-20 07:38:41 8 Comments

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


@naivists 2010-01-20 07:53:38

This counts as a different origin, even though you have it on the same box, just different port.

If you are targetting mostly new browsers like FireFox 3.5 and up, you can try to add Access-Control headers to your application in another port and allow to call from your default app pool. Information about access control headers can be found here:

IE also implements it (again, in using a different ACTIVEX control, why so?): and

@Daniel Vassallo 2010-01-20 07:52:49

This breaks the Same origin policy. You cannot use a different port, even when using the same domain.

You can use JSONP as Doug suggested.

Or else, as another possible workaround, you could set up a very simple reverse proxy (using mod_proxy if you are on Apache). This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location.

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

ProxyPass     /ajax/     http://www.localhost:8080/

In this case, you would request /ajax/test.xml with jQuery, but in fact the server would serve this by acting as a proxy to http://www.localhost:8080/test.xml internally.

If you are using IIS, you may want to use the Managed Fusion URL Rewriter and Reverse Proxy to set up a reverse proxy.

@Doug Neiner 2010-01-20 08:04:20

+1 For a fantastic answer. I answered the wrote "blah blah blah" canned response, and you provided a very viable solution. Thanks Daniel!

@user198729 2010-01-20 08:05:13

Oh,proxy will cause extra processing on server side.So I'll choose jsonP

@screenm0nkey 2011-07-28 08:48:48

I'd been scratching around for day trying to work out why I couldn't post data across ports before I found this. Thanks man.

@tObi 2015-05-27 23:14:56

hey, what about CORS?

@Doug Neiner 2010-01-20 07:49:45

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('', {
  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.

@user198729 2010-01-20 07:56:16

Is there a general way to replace way it can only work in production,because I use localhost in developing environment.

@Doug Neiner 2010-01-20 08:03:25

You should be able to use 'http://' + window.location.hostname + ':8080/url' to build the URL that will work both locally and online.

Related Questions

Sponsored Content

51 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

58 Answered Questions

[SOLVED] How to check whether a checkbox is checked in jQuery?

19 Answered Questions

[SOLVED] How can I refresh a page with jQuery?

31 Answered Questions

18 Answered Questions

[SOLVED] Abort Ajax requests using jQuery

31 Answered Questions

[SOLVED] How to manage a redirect request after a jQuery Ajax call

18 Answered Questions

[SOLVED] Wait until all jQuery Ajax requests are done?

13 Answered Questions

17 Answered Questions

[SOLVED] How do I send a cross-domain POST request via JavaScript?

10 Answered Questions

[SOLVED] How to send FormData objects with Ajax-requests in jQuery?

Sponsored Content