我有以下代码从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对象的代码很好但是在尝试解析它时遇到了麻烦(也就是拉出我想要的位)。我正在使用的回调似乎没有进行,并且想知道是否有人可以查看它并帮助我。
具体来说,我可以为我选择的任何功能添加回调吗?我是否必须对该功能做任何事情?
答案 0 :(得分:1)
据我所知,你没有将回调传递给你的函数:
function getTree(hash) {
你正在使用:
objectedJSON = getTree(hash, function () {
同样,这个函数没有回调参数:
function parseTree(hash) {
你正在使用:
var objects = parseTree('master', function () {
修改你的功能:
function getTree(hash, fn) { ... }
function parseTree(hash, fn) { ... }
然后在需要时使用fn
致电fn()
。
答案 1 :(得分:1)
我已修复代码以说明您将如何处理它。
function getTree(hash, cb) {
// notice that I copy the callback and hash references to have access to them in this
// function's closure and any subsequent closures, like the success and error
// callbacks.
var pathToTree, returnedJSON, cb = cb, hash = hash;
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;
// if anything was passed, call it.
if (cb) cb(json);
},
error: function (error) {
console.debug(error);
// an error happened, check it out.
throw error;
}
});
return returnedJSON;
}
function parseTree(hash) {
var objectedJSON, objectList = [], i, entry;
objectedJSON = getTree(hash, function (objectedJSON) {
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);
});
});
答案 2 :(得分:1)
添加第二个参数o getTree
函数。像
function getTree(hash, callback)
在Ajax选项中使用“jsopCallback”参数
$.ajax({
...
jsopCallback: callback,
...