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

JavascriptIframePostmessage

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnieveView Question on Stackoverflow
Solution 1 - JavascriptnieveView Answer on Stackoverflow