实现两个站点之间的回调 - 两个站点之间的通信

发布于 2024-10-10 07:54:07 字数 1091 浏览 0 评论 0原文

我在 http://domain1/app1/used.html 中有一个网络应用程序,我想嵌入该应用程序位于 http://domain2/app2/caller.html 内,带有 iframe (或弹出窗口,它是相同)

用户应该能够与 Caller.html 进行交互,直到他们按下某个按钮,在这种情况下,我需要告诉 caller.html 用户从 Caller.html 中选择了一个项目,

我尝试用 javascript 实现它。

在 Called.html 中,我将数据编码为 json,然后在 caller.html 中执行“used_callback”javascript 函数,将 json 作为参数传递。

如果 Caller.html 是通过弹出窗口调用的,我会发出 window.opener.used_callback( jsonData ),如果它是一个 iframe,我只需发出 Parent.used_callback( jsonData ),

其中 caller.html 和 Caller.html 在同一域中,一切正常,但从不同的域我得到以下错误:

permission denied (on IE6)

是否

Unsafe JavaScript attempt to access frame with URL [..]/caller.html from frame with URL [...]called.html. Domains, protocols and ports must match. (on google chrome)

有可能克服这个限制?

您还能想到什么其他方法来实现它?

我猜 caller.html 可以实现一项 Web 服务,并且我可以发送调用它的结果,但是页面 caller.html 必须进行轮询才能检测到任何更改...

那么一个应用程序如何与另一个应用程序进行通信不同的域来发出事件信号???

I have a web app in http://domain1/app1/called.html, and I want to embed that application inside http://domain2/app2/caller.html with an iframe (or a popup, it's the same)

the user should be able to interact with called.html, until they press a certain button, in that case I need to tell caller.html that the user selected an item from called.html

I tried implementing it with javascript.

in called.html I encode the data in json, and then I execute a "called_callback" javascript function in caller.html, passing the json as a parameter.

if called.html was called with a popup, I issue window.opener.called_callback( jsonData ), if it's an iframe I just issue parent.called_callback( jsonData )

having caller.html and called.html in the same domain everything works fine, but from different domains I get the following errors:

permission denied (on IE6)

and

Unsafe JavaScript attempt to access frame with URL [..]/caller.html from frame with URL [...]called.html. Domains, protocols and ports must match. (on google chrome)

Is it possible to overcome this limitation?

What other way of achieving it can you think of???

I guess caller.html could implement a web service, and I could send the result calling it, but the page caller.html would have to poll in order to detect any change...

So how can one application communicate with another one in a different domain to signal an event???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

っ〆星空下的拥抱 2024-10-17 07:54:07

您可以使用 JSONP 从一个域调用资源到另一个。

您可以使用 window.name 对于旧版浏览器,跨域框架之间的文本传输约为 2Mb。

或者对于现代浏览器,您可以使用 window.postMessage 在两者之间传递字符串数据帧。

但你需要一些领域的合作才能使这些技术发挥作用。

You can use JSONP to call resources from one domain to another.

You can use window.name as ~2Mb text transfer between cross domain frames for older browser.

Or for modern browser you can use window.postMessage to communicate string data between the 2 frames.

But you need some cooperation from the domains for these techniques to work.

寄居者 2024-10-17 07:54:07

您应该考虑使用 JSONP。如果您使用特定的框架,jQuery 完全支持它。它允许您跨域使用 JSON。

You should look into using JSONP. It is fully supported in jQuery if you are using that particular framework. It allows you to use JSON across domains.

美人如玉 2024-10-17 07:54:07

感谢这两个答案,我发现了以下内容:

http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

http://benalman.com/projects/jquery-postmessage-plugin/

jQuery postMessage 使简单和
轻松的window.postMessage通信
在支持它的浏览器中(FF3、
Safari 4、IE8),同时回退到
document.location.hash 通信
适用于所有其他浏览器(IE6、
IE7、Opera)。

添加了
window.postMessage 方法,JavaScript
终于有了一个绝妙的方法
跨域帧通信。
不幸的是,这个方法并不
所有浏览器都支持。一个例子
这个插件有用的地方是当
子 Iframe 需要告诉其父级
它的内容已调整大小。

我去看看...

Thanks to both answer I found the following:

http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

http://benalman.com/projects/jquery-postmessage-plugin/

jQuery postMessage enables simple and
easy window.postMessage communication
in browsers that support it (FF3,
Safari 4, IE8), while falling back to
a document.location.hash communication
method for all other browsers (IE6,
IE7, Opera).

With the addition of the
window.postMessage method, JavaScript
finally has a fantastic means for
cross-domain frame communication.
Unfortunately, this method isn’t
supported in all browsers. One example
where this plugin is useful is when a
child Iframe needs to tell its parent
that its contents have resized.

I'll have a look at it...

南巷近海 2024-10-17 07:54:07

这是一个非常完整的文档,分析了不同的方法...

http://softwareas.com/ cross-domain-communication-with-iframes

另一个解决方案,看看

http://easyxdm.net/

带有示例

http://easyxdm.net /wp/2010/03/17/设置你的第一个套接字/

here's a very complete document that analizes the different approaches...

http://softwareas.com/cross-domain-communication-with-iframes

another solution to have a look at

http://easyxdm.net/

with a sample

http://easyxdm.net/wp/2010/03/17/setting-up-your-first-socket/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文