跨域XMLHttp请求
这是我的情况:
我有一台 Web 服务器机器、一台客户端机器和第三台运行一些侦听 XMLHttpRequest 的程序的机器。
客户端从客户端计算机访问网络服务器,进行一些更改,然后单击“保存”。此时,数据被发送回网络服务器和第三台机器。所有这些都是使用 Javascript 和 XMLHttpRequest 对象完成的。
发布到网络服务器工作正常,但是发布到第三台机器不起作用,因为它有不同的 IP/域。
我怎样才能做到这一点? 客户端机器->第三台机器无法工作,因为它位于不同的域中 网络服务器机器 ->由于防火墙问题,第三台机器无法工作
任何想法都将不胜感激!
Here is my situation:
I have a Webserver machine, Client machine, and a third machine running some program that listens for XMLHttpRequests.
Client accesses the Webserver from the Client machine, makes some changes, and then clicks on'Save'. At this point, data is being sent back to the Webserver and to the Third machine. All of this is being done using Javascript and XMLHttpRequest object.
The post to the Webserver works fine, however post to the Third machine does not work, since it had a different IP/domain.
How can I make this work?
Client machine -> Third machine does not work, because its on a different domain
Webserver machine -> Third machine does not work, because of firewall issues
Any ideas are greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您遇到的是同源政策,而不是防火墙问题。
如果计算机共享顶级域,您可以设置其 document.domain要匹配的属性:
否则,您可能需要设置第一个主机来将请求代理到其他主机。
What you're running into is the Same origin policy, not firewall issues.
If the machines share a top-level domain, you can set their document.domain properties to match:
Otherwise, you may need to setup your first host to proxy the request to the other host.
您可以实施
CORS
( 跨源资源共享)在您的服务器上。当前版本的主要桌面浏览器已经支持它一段时间了,Opera 和 Explorer 10 是最后添加支持的浏览器。
不过,我不确定目前跨移动浏览器对 CORS 的支持有多广泛。
You can implement
CORS
(Cross-Origin Resource Sharing) on your server.The current versions of the major desktop browsers have supported it for a while, Opera and Explorer 10 being the last ones to add support.
I'm not sure how widely supported CORS is across mobile browsers at this time though.
您还可以使用iframe hack。第二个域托管的 iframe 可以在第一个域托管的页面中呈现,并且 窗口iframe 的对象 可以从父页面的上下文中访问。
You could also use the iframe hack. An iframe hosted by the second domain can be rendered in a page hosted by the first domain, and the window object for the iframe will be accessible from the parent page's context.
另一种选择(诚然有点技术含量)是如果数据不敏感,则使用 YQL 。您可以使用 YQL 和JSON-P 以便也从其他域获取数据。
Another option (which is admittedly a bit techie) is to use YQL if the data isn't sensitive. You can use YQL & JSON-P in order to fetch data from other domains as well.