未捕获的 DOMException: 未能在“ Window”上执行“ postMessage”: 无法克隆对象

我正在打电话

parent.postMessage(obj, 'whatever');

从一个 iframe 内部,我得到这个错误: Uncaught DOMException: Failed to execute 'postMessage' on 'Window': An object could not be cloned.

119399 次浏览

It turns out the object I passed had methods, which is why the error message said An object could not be cloned.

In order to fix this, you can do the following:

obj = JSON.parse(JSON.stringify(obj));
parent.postMessage(obj, 'whatever');