Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

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

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...