我已經看過這個帖子: Jquery - JSON.stringify, array is empty ,也與其他論壇圍繞檢查並搜查谷歌並沒有找到一份像樣的答案。json.stringify返回空白[]一個對象,而不是一個充滿{}
基本上,當我在我用json.stringify保存[]數組,它正確地創建JSON字符串上的一切,但一個數組值,這將是在保存[2]。只是這裏的例子是怎樣的字符串化的職位均爲1和2的數組中:
"1":{"doodleID":"716","tid":"26","outline":"sites/default/files/doodlekin/female24solid.swf","skin":"sites/default/files/doodlekin/female24face.swf","hair":"sites/default/files/doodlekin/female24hair.swf","misc":"sites/default/files/doodlekin/female24lips.swf","addlayer1":"","addlayer2":"","addlayer3":"","doodlename":"Mom","headsize":"95","flip":"true","skincolor":"E8B18C","skinblush":"sites/default/files/skin_color/e8b18c.swf","haircolor":"855D33","mouth":"sites/default/files/mouth/lips_color_d6570a_1.swf","neck":"sites/default/files/neck/pearl_necklace_solid.swf","neck_optlayer":"sites/default/files/neck/pearl_necklace_color.swf","neck_optcolor":"f8e5be"},
"2":[]
它寫出一個正確的,但是當涉及到2的值沒有被正確地解析成JSON字符串。所以你知道保存的數組在1和2的值看起來是什麼樣的(這個數組更大,我只是認爲這應該是你需要知道什麼是錯誤的,如果沒有,請讓我知道。的,而不是2):
'1' ...
'doodleID' => "716"
'tid' => "26"
'outline' => "sites/default/files/doodlekin/female24solid.swf"
'skin' => "sites/default/files/doodlekin/female24face.swf"
'hair' => "sites/default/files/doodlekin/female24hair.swf"
'misc' => "sites/default/files/doodlekin/female24lips.swf"
'addlayer1' => ""
'addlayer2' => ""
'addlayer3' => ""
'doodlename' => "Mom"
'headsize' => "95"
'flip' => "true"
'skincolor' => "E8B18C"
'skinblush' => "sites/default/files/skin_color/e8b18c.swf"
'haircolor' => "855D33"
'mouth' => "sites/default/files/mouth/lips_color_d6570a_1.swf"
'neck' => "sites/default/files/neck/pearl_necklace_solid.swf"
'neck_optlayer' => "sites/default/files/neck/pearl_necklace_color.swf"
'neck_optcolor' => "f8e5be"
'2' ...
'doodleID' => "291"
'tid' => "27"
'outline' => "sites/default/files/doodlekin/male66solid.swf"
'skin' => "sites/default/files/doodlekin/male66face.swf"
'hair' => "sites/default/files/doodlekin/male66hair.swf"
'misc' => ""
'addlayer1' => ""
'addlayer2' => ""
'addlayer3' => ""
'doodlename' => "John"
'headsize' => "95"
'skincolor' => "EEBF7C"
'skinblush' => "sites/default/files/skin_color/eebf7c.swf"
'haircolor' => "353535"
'eyes' => ""
'eyes_shadow' => ""
'eyes_optlayer' => ""
'eyes_optcolor' => ""
'mouth' => "sites/default/files/mouth/goatee_solid.swf"
'mouth_optlayer' => "sites/default/files/mouth/goatee_color.swf"
'mouth_optcolor' => "646464"
我的問題是爲什麼被保存[2]被從保存取出時使用: POSTDATA = JSON.stringify(保存);
但保存陣列仍然存在時,它顯示2.任何幫助將是巨大的滿,如果你需要更深入的瞭解,我將它送給你。我使用全局變量的原因是因爲我使用了大約10個需要調用該變量的不同函數,我不想通過設計器忽略它。
更新:剛剛意識到這個問題只用Firefox發生得。我正在運行firefox 4 Beta 7.任何可能成爲json.stringify無法正常工作的原因的機會? IRT與Chrome和IE 8.
更新2的工作:用於整個節省:
function userSavedDoodlekin(flag)
{
// For leave page check
addToCartPage = "true";
pID = $("#neededInfo .productID").text();
tID = $("#neededInfo .templateID").text();
pAttr = new Object();
tBg = $("#neededInfo .templateBG").text();
tCoords = $("#neededInfo .templateCoords").text();
$("#neededInfo .productAttr").each(function(){
pAttr[$(this).attr('alt')] = $(this).text();
});
store_doodlekin("product", pID, "productID");
store_doodlekin("product", pAttr, "productAttr");
store_doodlekin("product", tID, "templateID");
store_doodlekin("product", tBg, "templateBG");
store_doodlekin("product", tCoords, "templateCoords");
// Data for post "json" serialized
postData = JSON.stringify(saved);
flag = '';
// Check for flag
if(flag == "true")
{
flag = '?saved=true';
}
// Test before launch
// alert(postData);
// alert(dump(saved));
// Save the doodlekin and start another loop for the next one to save if another in line.
// Data Sent Back: object.status = (1 or 0), object.data = (error message)
$.ajax({
type: 'POST',
dataType: 'json',
data: "data="+postData,
url: "/designer/save",
success: function(obj){
if(obj.status == 1){
// alert(obj.data);
window.location = "/cart";
}
else if(obj.status == 0){
alert(obj.data);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("textStatus: "+textStatus);
alert("errorThrown: "+errorThrown);
}
});
}
這可能是一個錯字的地方,可能是數據本身。你能在JSON創建並顯示的地方發佈代碼嗎?完成括號,括號和分號。編寫工作(或失敗)最小問題示例的行爲經常會揭示問題出在哪裏。 – 2010-12-09 18:04:29
查看帖子的更新,只是我看到它不是這個原因,是因爲現在我知道它適用於Firefox 3.5 + 3.6以及IE 8和Chrome。我只是在我的工作電腦Firefox 4 Beta 7的Firefox中遇到了這個問題。除此之外,當數組被保存時,它在其他瀏覽器上工作得很好。 – John 2010-12-09 20:17:51