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

user198729 picture user198729 · Jan 20, 2010 · Viewed 66.7k times · Source

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

Answer

Daniel Vassallo picture Daniel Vassallo · Jan 20, 2010

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.