文字列としてシリアライズしたい JavaScript オブジェクトがあります。
key {...} Object
mandant "00001" String
personalNummer 600235 Number
まず、戻り値が である JSON2 を使用しましたundefined
。JSON3を使用すると、次TypeError
の行のコメントが表示されjson3.js
ます。
// Cyclic structures cannot be serialized by `JSON.stringify`.
この問題は、json3.jsの次の行に起因するようです:
// Manually invoke the callback for the `constructor` property due to
// cross-environment inconsistencies.
if (isConstructor || isProperty.call(object, (property = "constructor"))) {
callback(property);
}
しかし、サイクルがあってはなりません。一体何が起こっているのかを知ることができないのは明らかです。
デバッグ中に手動でオブジェクトを作成すると、すべて正常に動作します。
では、何がエラーを引き起こす可能性がありますか?
編集:
エラーを生成するシナリオの準備に成功しました:
- IE7 および IE8 互換モードの IE9 で発生します (Firefox 22 でも問題ありません)。
- オープナーウィンドウからのデータを参照する新しいウィンドウが開かれた場合に発生します
*JSON_Cycle.html*:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://bestiejs.github.io/json3/lib/json3.js"></script>
<script>
var dataGlobal = {mandant: "Hallo Welt!", personalNummer: 123456};
$(function() {
window.open("JSON_Cycle_Popup.html", 'popup');
});
</script>
*JSON_Cycle_Popup.html*:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://bestiejs.github.io/json3/lib/json3.js"></script>
<script>
var dataGlobal = null;
$(function() {
dataGlobal = window.opener.dataGlobal;
alert(JSON.stringify(dataGlobal));
});
</script>