Javascript “DataCloneError:无法克隆对象。” 在 Firefox 34
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27558398/
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
"DataCloneError: The object could not be cloned." in FireFox 34
提问by Krishna
Using given function to post message, but getting error "DataCloneError: The object could not be cloned." at Line "target['postMessage'](message, target_url.replace( /([^:]+://[^/]+).*/, '$1'));" in FireFox-34, same code is working fine on Chrome and older version of FireFox.
使用给定函数发布消息,但收到错误“DataCloneError:无法克隆对象。” 在行“target['postMessage'](message,target_url.replace(/([^:]+://[^/]+).*/,'$1'));” 在 FireFox-34 中,相同的代码在 Chrome 和旧版本的 FireFox 上运行良好。
var storage = function() {
return {
postMessage : function(message, target_url, target) {
if (!target_url) {
return;
}
var target = target || parent; // default to parent
if (target['postMessage']) {
// the browser supports window.postMessage, so call it with a targetOrigin
// set appropriately, based on the target_url parameter.
target['postMessage'](message, target_url.replace( /([^:]+:\/\/[^\/]+).*/, ''));
}
}
}
}();
回答by Michael Thompson
postMessagesends messages using the structured clone algorithmin Firefox and because of that there are certain things you need to adjust prior to sending.
postMessage使用Firefox 中的结构化克隆算法发送消息,因此在发送之前您需要调整某些事情。
In your example it isn't obvious what message contains but one hack-y way around structured clone is to coerce a bit. Sending a URL via postMessagewill throw an error:
在您的示例中,消息包含的内容并不明显,但一种绕过结构化克隆的黑客方法是强制执行一点。通过发送 URLpostMessage将引发错误:
someWindow.postMessage(window.location, '*');
// ERROR
But you can do this to work around it:
但是你可以这样做来解决它:
var windowLocation = '' + window.location;
someWindow.postMessage(windowLocation, '*');
// WORKS
There are better ways to handle this but for what you've provided this should at least allow consistent behavior.
有更好的方法来处理这个问题,但对于您提供的内容,这至少应该允许一致的行为。

