我正在尝试通过发送包含字符串化对象的消息来使两个 React 应用程序进行通信。
父级(http://localhost:3000)通过 postMessage 发送消息,如下所示:
let iframe = document.querySelector("iframe")
useEffect(() => {
if (!!localStorageObject && !!iframe) {
iframe?.contentWindow?.postMessage(localStorageObject, "http://localhost:3001")
}
}, [localStorageObject, iframe])
// ...file and start of return
iFrame (http://localhost:3001) 没有收到消息,至少没有立即收到消息(我必须等待反应软刷新才能显示日志)。
首先抛出的错误是:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:3001') does not match the recipient window's origin ('http://localhost:3000'). Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号