javascript 使用 easyXDM 在父文档和从不同域(亚马逊)加载的子 iframe 之间进行通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8661243/
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-10-26 04:08:47  来源:igfitidea点击:

Using easyXDM to communicate between parent document and child iframe loaded from a different domain (amazon)

javascripthtmliframecross-domain

提问by ivymike

I'm trying to use easyXDM to communicate between parent document and child iframe (loaded from a different domain - amazon). The iframe src is an oauth signed url and has the following code to communicate with the parent document that loads it:

我正在尝试使用 easyXDM 在父文档和子 iframe(从不同的域加载 - 亚马逊)之间进行通信。iframe src 是一个 oauth 签名的 url,并具有以下代码来与加载它的父文档进行通信:

socket = new easyXDM.Socket({
    remote: "http://localhost:56789/hitch.html", /* parent document */
    remoteHelper: "http://localhost:56789/easyXDM/name.html",
    onMessage: function(message, origin){
        alert("Received '" + message + "' from '" + origin + "'");
    },
    onReady: function() {
        socket.postMessage("Yay, it works!");
    }
});

the above code is kept in the head portion of the document.

上面的代码保存在文档的头部。

In parent (hitch.html):

在父级 (hitch.html) 中:

var transport = new easyXDM.Socket(/** The configuration */{
    local: "/easyXDM/name.html",
    swf: "/easyXDM/easyxdm.swf",
    onMessage: function(message, origin){
       transport.postMessage("This is a message received from " + location);
    }
});

When I load the child iframe from amazonS3 inside the parent document, easyXDM is creating another iframe inside the child iframe with src set to "http://localhost:56789/hitch.html?xdm_e=..." . This causes the whole thing to be repeated in a cycle - with parent again trying to load the child iframe and so on.

当我从父文档中的 amazonS3 加载子 iframe 时,easyXDM 正在子 iframe 中创建另一个 iframe,并将 src 设置为 "http://localhost:56789/hitch.html?xdm_e=..." 。这导致整个事情在一个循环中重复 - 父再次尝试加载子 iframe 等等。

I'm testing on Firefox 9.0 which has postMessage support. The actual messages are being sent properly and I can see the message boxes. Other than this, it also throws a "url is undefined or empty" error in parent document when initializing easyXDM.socket but it doesn't seem to affect anything else...

我正在测试支持 postMessage 的 Firefox 9.0。实际消息正在正确发送,我可以看到消息框。除此之外,它还会在初始化 easyXDM.socket 时在父文档中抛出“url is undefined or empty”错误,但它似乎不会影响其他任何东西......

Thanks,

谢谢,

采纳答案by chrismarx

I think you've just got the logic backwards. The documentation says quite clearly:

我认为你的逻辑倒退了。文档说得很清楚:

"When using easyXDM you first load the consumer document and then let easyXDM load the provider."

“使用 easyXDM 时,您首先加载消费者文档,然后让 easyXDM 加载提供者。”

The "consumer" is the parent document, and easyxdm loads the "provider" which is the child iframe.

“消费者”是父文档,easyxdm 加载“提供者”,即子 iframe。

ref - https://github.com/oyvindkinsey/easyXDM

参考 - https://github.com/oyvindkinsey/easyXDM