我在$.post()
的回调函数中有一个fetch()
。fetch()
从后端获取数据并很好地更新集合,但是当它运行success
或error
回调时,什么都不会发生!我在它的两个回调中都放置了console.log()
,它们从未出现在Javascript控制台中。
知道发生了什么事吗?
视图中的方法
create_set: function() {
var self = this;
// Post data to server
$.post('api/create_set', {
user_id: $('#user_id').val(),
post_id: this.post_id,
set_name: $('#new_set_name').val()
}, function() {
// Update list of Sets
self.setList.fetch({
data: {
user_id: $('#user_id').val(),
post_id: this.post_id
},
processData: true
}, {
success: function() {
// Highlight the first class in list
$(self.setListView.el).children('div:first').addClass('active');
console.log('success'); // DOESNT RUN!
}
}, {
error: function() {
console.log('error'); // DOESNT RUN!
}
});
console.log('hello'); // RUNS!
});
}
success
和error
应该是传递给fetch
的options
对象的属性,不必为它们创建单独的对象:
self.setList.fetch({
data: {...},
processData: true,
success: function(){...},
error: function(){...}
})