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

时间:2022-08-25 17:56:21

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

我需要向端口8080发送一个AJAX请求,在那里有一个守护进程在运行。

3 个解决方案

#1


31  

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.

不能发布跨域、子域或端口号的信息。但是,如果您同时访问守护进程和请求站点,则可以使用JSONP。如果需要返回数据,那么守护进程需要支持回调查询参数并返回正确格式化的数据。

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:

现在只需确保守护进程处理回调参数。例如,如果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进行跨域通信。

#2


38  

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.

您可以像Doug建议的那样使用JSONP。

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.

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

The fundamental configuration directive to set up a reverse proxy in mod_proxy is 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.xml with jQuery, but in fact the server would serve this by acting as a proxy to http://www.localhost:8080/test.xml internally.

在这种情况下,您将请求/ajax/测试。使用jQuery的xml,但实际上,服务器可以通过在内部代理http://www.localhost:8080/test.xml来实现这一点。

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

如果您正在使用IIS,您可能希望使用托管的Fusion URL Rewriter和反向代理来设置反向代理。

#3


2  

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: https://developer.mozilla.org/en/HTTP_access_control

如果大多数浏览器都是新的,比如FireFox 3.5或更高版本,那么可以尝试在另一个端口向应用程序添加访问控制头,并允许从默认的应用程序池中调用。有关访问控制头的信息可以在这里找到: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.aspx and 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.aspx和http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx)

#1


31  

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.

不能发布跨域、子域或端口号的信息。但是,如果您同时访问守护进程和请求站点,则可以使用JSONP。如果需要返回数据,那么守护进程需要支持回调查询参数并返回正确格式化的数据。

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:

现在只需确保守护进程处理回调参数。例如,如果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进行跨域通信。

#2


38  

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.

您可以像Doug建议的那样使用JSONP。

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.

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

The fundamental configuration directive to set up a reverse proxy in mod_proxy is 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.xml with jQuery, but in fact the server would serve this by acting as a proxy to http://www.localhost:8080/test.xml internally.

在这种情况下,您将请求/ajax/测试。使用jQuery的xml,但实际上,服务器可以通过在内部代理http://www.localhost:8080/test.xml来实现这一点。

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

如果您正在使用IIS,您可能希望使用托管的Fusion URL Rewriter和反向代理来设置反向代理。

#3


2  

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: https://developer.mozilla.org/en/HTTP_access_control

如果大多数浏览器都是新的,比如FireFox 3.5或更高版本,那么可以尝试在另一个端口向应用程序添加访问控制头,并允许从默认的应用程序池中调用。有关访问控制头的信息可以在这里找到: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.aspx and 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.aspx和http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx)