![](/img/trans.png)
[英]I have nested Json data stored in variables how do I access them in another function in jquery?
[英]json request with data variables - how do I read the variable in the function?
從名為bundleOfData的數組中加載一堆json文件。 .url是每個json文件的url。
如何在processData函數中讀取變量“ myI”?
for(var i = 0; i < bunchOfData.length; i++){
$.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(processData);
function processData(ds){
console.log(ds.myI); //undefined
}
}
function successHandler(i){
console.log('i is instantiated/stored in the closure scope:',i);
return function(data, textStatus, jqXHR){
// here, the server response and a reference to 'i'
console.log('i from the closure scope:',i, 'other values',data,textStatus,jqXHR);
}
}
var i = 0; i < bunchOfData.length; i++){
$.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(successHandler(i));
}
您不能訪問范圍之外的變量,響應僅是有效的
function(ds){
// Here
}
嘗試這個:
for(var i = 0; i < bunchOfData.length; i++){
$.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){
console.log(ds);
});
}
此外,一旦請求完成並且是全成內容success
運行,請注意,你正在嘗試才達到可以與單個請求不要(你可以統一在一個單一的文件和SERV它們),所以你得到的數據然后進行迭代。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.