Javascript 未捕获的 DOMException:无法在“窗口”上执行“postMessage”:无法克隆对象

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

Uncaught DOMException: Failed to execute 'postMessage' on 'Window': An object could not be cloned

javascriptiframepostmessage

提问by nieve

I'm trying to call

我想打电话

parent.postMessage(obj, 'whatever');

from within an iframe and I'm getting this error: Uncaught DOMException: Failed to execute 'postMessage' on 'Window': An object could not be cloned.

从 iframe 中,我收到此错误: Uncaught DOMException: Failed to execute 'postMessage' on 'Window': An object could not be cloned.

回答by nieve

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

原来我传递的对象有方法,这就是为什么错误信息说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');