我有以下代码从github中获取JSON对象,并且正在尝试将某些部分添加到数组中。回调函数似乎没有执行
function getTree(hash) {
var pathToTree, returnedJSON;
pathToTree = 'https://api.github.com/repos/myaccount/myrepo/git/trees/' + hash;
$.ajax({
accepts: 'application/vnd.github-blob.raw',
dataType: 'jsonp',
url: pathToTree,
success: function (json) {
returnedJSON = json;
},
error: function (error) {
console.debug(error);
}
});
return returnedJSON;
}
function parseTree(hash) {
var objectedJSON, objectList = [], i, entry;
objectedJSON = getTree(hash, function() {
console.debug(objectedJSON); // this is not appearing in console
for (i = 0; i < objectedJSON.data.tree.length; i += 1) {
entry = objectedJSON.data.tree[i];
console.debug(entry);
if (entry.type === 'blob') {
if (entry.type.slice(-4) === '.svg') { // we only want the svg images not the ignore file and README etc
objectList.append(i.content);
}
} else if (entry.type === 'tree') {
objectList.append(parseTree(getTree(entry.sha)));
}
}
});
return objectList;
}
$(document).ready(function() {
var objects = parseTree('master', function() {
console.debug(objects);
});
});
我的代码检索JSON对象不错,但我试图把它解析(又名拉出我想要的位)时遇到了麻烦。我正在使用的回调似乎没有进行,并想知道是否有人可以看看它并帮助我。
具体来说,我可以添加一个回调到我选择的任何函数吗?我必须为这个功能做什么吗?