小编典典
在您的代码中,您正在寻找groups在进行ajax调用之后使用过程编码的方法。主要问题是groups在ajax调用完成之前正在寻找。
另一个问题是您要向该success()函数返回组,但是该TheObject.getArray()函数什么也不返回。
因此,您需要像这样将回调引入ajax函数:
TheObject = {
getArray: function(callback) {
var groups = new Array;
$.ajax({
type: "POST",
url: "link.php",
success: function (data){
var counter = 0;
$('g',data).each(function(){
var group_name = $(this).find("name").text();
var group_id = $(this).find("id").text();
var group = {
id: group_id,
name: group_name
}
groups[counter] = group;
counter++;
});
callback.call(this,groups);
}
});
}
}
TheObject.getArray(function(a) {
// this code runs when the ajax call is complete
alert(a);
});
2020-07-26