gpt4 book ai didi

javascript - 实现跨域通信

转载 作者:行者123 更新时间:2023-11-30 18:09:05 25 4
gpt4 key购买 nike

我正在尝试使用 TypeScript 实现跨域通信,如 this thread 中所述

我的代码如下:

 var iframeElement = document.getElementById("myIframe");
iframeElement.document.contentWindow.postMessage('hello', '*');

但是在文档之后的第二行我没有得到contentwindow 的句柄。

有人可以提供解决方案吗?

最佳答案

如果您所说的“处理”是指您没有完成代码,那是因为 contentWindowiframeElement 的属性,而不是文档的属性(并且顺便说一下,因为 document.getElementById 的结果是一个通用的 HtmlElement,而不是 iframe)。

试试这个:

// Cast the result of getElementById, so you get an iframe and not a generic HtmlElement:
var iframeElement:HTMLIFrameElement = <HTMLIFrameElement>document.getElementById("myIframe");
// Then reference the contentWindow property of the iframe element
iframeElement.contentWindow.postMessage('hello', '*');

关于javascript - 实现跨域通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15072630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com