Javascript 如何使用 jQuery 在不同端口上发送 AJAX 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:46:31  来源:igfitidea点击:

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

javascriptjqueryajaxcross-domain

提问by user198729

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

例如,我需要向运行守护程序的端口 8080 发送 AJAX 请求。

采纳答案by Doug Neiner

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

您不能POST跨域、子域或端口号提供信息。但是,如果您有权访问守护程序请求站点,则可以使用 JSONP 。如果需要返回数据,则daemon需要支持callback查询参数并以正确的格式返回。

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 callbackparameter. For instance, if callback=mycallbackthe return from the daemon (the only thing written to the page) should look like this:

现在只需确保您的守护进程处理callback参数。例如,如果callback=mycallback守护进程的返回(写入页面的唯一内容)应该如下所示:

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.

如果您没有 JSONP 或类似的机制,则无法使用 jQuery 进行跨域通信。

回答by Daniel Vassallo

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

这打破了同源策略。您不能使用不同的端口,即使使用相同的域也是如此。

You can use JSONP as Dougsuggested.

您可以按照Doug 的建议使用 JSONP 。

Or else, as another possible workaround, you could set up a very simple reverse proxy(using mod_proxyif 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.

或者,作为另一种可能的解决方法,您可以设置一个非常简单的反向代理(如果您使用的是 Apache,请使用mod_proxy)。这将允许您在 AJAX 请求中使用相对路径,而 HTTP 服务器将充当任何“远程”位置的代理。

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

mod_proxy 中设置反向代理的基本配置指令是 ProxyPass。您通常会按如下方式使用它:

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

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

在这种情况下,您将/ajax/test.xml使用 jQuery请求,但实际上服务器将通过充当http://www.localhost:8080/test.xml内部代理来提供服务。

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

如果您使用的是 IIS,您可能需要使用Managed Fusion URL Rewriter 和反向代理来设置反向代理。

回答by naivists

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-Controlheaders to your application in another port and allow to call from your default app pool. Information about access control headers can be found here: https://developer.mozilla.org/en/HTTP_access_control

如果您主要针对 FireFox 3.5 及更高版本等新浏览器,您可以尝试Access-Control在另一个端口中向您的应用程序添加标头,并允许从您的默认应用程序池调用。可以在此处找到有关访问控制标头的信息:https: //developer.mozilla.org/en/HTTP_access_control

IE also implements it (again, in using a different ACTIVEX control, why so?): http://blogs.msdn.com/ie/archive/2009/01/14/completing-access-control-support-for-xdomainrequest.aspxand http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx

IE 也实现了它(同样,在使用不同的 ACTIVEX 控件时,为什么会这样?):http: //blogs.msdn.com/ie/archive/2009/01/14/completing-access-control-support-for-xdomainrequest .aspxhttp://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx