我有一个ajax请求如下:
jQuery.ajax({
type: 'GET',
url: '/logical_interface/delete',
context: this, // had to add this to get the line in the success function to work, never used it before nor do I understand why it works
data: 'id=' + id,
beforeSend: function() {
// jQuery(this).parent().html('processing...');
// if this line is uncommented then the DOM will be updated correctly
// but the snippet in the success function won't fire but the delete
// is still executed on the server side
// the page is then stuck with the 'processing' text
},
success: function(data) {
jQuery(this).closest('tr').stop().animate({ backgroundColor: '#ffc6be' }, 'fast').hide('slow');
}
});
更新
服务器端代码只是以下Rails方法:
def delete
@logical_interface = LogicalInterface.find(params[:id])
@logical_interface.destroy
render :text => '1' // which is what I get in console.log
end
答案 0 :(得分:1)
正如评论中所述,您成功可能不起作用的原因是您删除了$(this)节点。
您在beforeSend功能中所做的事情正在上升一级,并用“处理...”替换所有 HTML。在成功案例到达之前,这又从DOM中删除了您的参考点jQuery(this)
。如果jQuery(this)
被删除,则没有任何事情发生(显而易见)。
不是用Processing覆盖整个html,我建议你隐藏一个元素,直到你触发ajax并在发送之前显示它并用完整的函数隐藏它。