我有一個函數,使嵌套的JSON對象的GET請求。該對象成功返回,但不能訪問返回對象內的其他對象。通過或返回JSON對象從jQuery獲取
物體看起來是這樣的:
{
"student": {
"hobbies": ["reading", "dancing", "music"],
"subjects": ["english", "maths", "science"]
}
}
,這是功能:
var superObject = {
getData: function(obj) {
$.get(obj.target, function(callbackObject) {
// It works fine if i log callbackObject
// console.log(callbackObject);
return callbackObject;
}
},
useData: function() {
var data = superObject.getData({'target': 'file.json'});
var hobbies = data.student.hobbies;
console.log(hobbies); // This fails and returns nothing.
}
}
你的邏輯爲我工作。我剛剛讀了一些關於**異步**,它有幫助。非常感謝。 –