0
我有关于ajax
callback
功能JavaScript的AJAX回调函数不工作
一个问题,我有
test = function(){
this.items=[];
}
//original state.
test.prototype.getListItems = function(){
this.items.push('test item');
this.handleItems();
}
//this method will be called if the user clicks a button.
test.prototype.clickBtn = function(){
this.ajGetOneMoreItem()
this.handleItems();
}
//a callback function that will add a new item.
test.prototype.ajGetOneMoreItem = function(){
var that=this;
ajax.callback = function(dataItem){
that.items.push(dataItem);
}
}
//to show the items.
test.prototype.handleItems = function(){
//only show the test item, but not dataItem after user clicks a button.
console(this.items)
}
var testObj = new test();
$('#testBtn).click(function(){
testObj.clickBtn();
})
我想表明,通过用户添加新的项目。 但是,它只出现this.items只显示第一个'test item
',但没有添加新的dataItem
。我在这里错过了什么吗?非常感谢!
您的AJAX调用很可能是异步的。这意味着'this.handleItems()'将在AJAX响应新数据之前运行。在你的回调之类的地方做一些日志记录,你会发现代码运行不正常。 – 2013-04-05 22:38:48
这必定是有数百个重复的世纪的问题。 – elclanrs 2013-04-05 22:38:57