首页 > 解决方案 > 为什么我会得到“在 'Window' 上执行 'postMessage' 失败:#使用 iframe 的 Promise 时无法克隆错误”?

问题描述

我有主机 Web 应用程序,它在 iframe 中加载具有相同域的同一应用程序的另一部分。我的代码如下所示:

const iframe = document.querySelector('iframe');
const api = iframe.contentWindow.api;

const result = await api.performAsyncAction().then(response => response.json());

执行上面的代码后,我收到以下错误消息:

Failed to execute 'postMessage' on 'Window': #<Promise> could not be cloned.

你能帮我解决这种错误吗?我知道 postMessage 用于在 iframe 和主机应用程序之间进行通信,但我需要弄清楚为什么会抛出上面的错误(我有点惊讶,因为我在这里不使用 postMessage)

更新

performAsyncAction方法的实现如下:

function performAsyncAction () {
  return fetch("/api/request", {
        method: "POST",
        headers: {
           Accept: 'application/json'   
        },
    })
}

标签: javascriptiframepromisepostmessage

解决方案


推荐阅读