根據文檔,jQuery.extend()是同時執行JSON對象的深度和淺度副本的解決方案。但是,當我使用這個,我得到一個未定義的對象錯誤。JSON對象的jQuery克隆返回undefined
我的Ajax請求的功能和處理程序:
var tourData;
$.ajax({
type: "GET",
url: "includes/phpscripts.php?action=stops",
dataType: "json",
success: (function(data){
if (data == 'false')
console.log("Can't load initial panorama");
else
processOptions(data);
})
});
function processOptions(data){
tourData = jQuery.extend(true, {}, data);
console.log(data.length);
console.log(tourData.length);
}
在Firebug中,data.length
返回6,這是我所期待的。但tourData
返回未定義。這發生在有和沒有true
作爲深拷貝的參數
我將需要這個請求中的數據以後可用於幾個函數,這些函數將超出範圍。因此,我希望有一個克隆的響應可用。
的data
內容是
[
{"fileName":"..\/panos\/photos\/1-prefix_blended_fused.jpg","name":"Start","lat":"43.682211","lon":"-70.450705","heading":"250","width":"1808","height":"653"},
{"fileName":"..\/panos\/photos\/2-prefix_blended_fused.jpg","name":"Second","lat":"43.6822","lon":"-70.450769","heading":"250","width":"1600","height":"578"},
{"fileName":"..\/panos\/photos\/2-prefix_blended_fused.jpg","name":"Second","lat":"43.6822","lon":"-70.450769","heading":"250","width":"1600","height":"578"},
{"fileName":"..\/panos\/photos\/3-prefix_blended_fused.jpg","name":"Third Stop","lat":"43.682219","lon":"-70.450828","heading":"250","width":"1821","height":"627"},
{"fileName":"..\/panos\/photos\/4-prefix_blended_fused.jpg","name":"Fourth Stop","lat":"43.68218","lon":"-70.450887","heading":"250","width":"1600","height":"800"},
{"fileName":"..\/panos\/photos\/5-prefix_blended_fused.jpg","name":"Last Stop","lat":"43.682165","lon":"-70.450933","heading":"250","width":"1808","height":"673"}
]
datA的內容是什麼 –
好像'data'是一個數組。 'console.log(data)',讓我們看看裏面有什麼。 –
是的,數據是一個數組,而不是一個對象。你想要'$ .extend'在這裏做什麼?你想要的輸出是什麼? –