1

我有一个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
4

1 回答 1

1

正如评论中提到的,您的成功可能不起作用的原因是您删除了 $(this) 节点。

您在 beforeSend 函数中所做的事情是上升一级并将所有HTML 替换为“处理...”。这反过来又jQuery(this)在达到成功案例之前从 DOM 中删除了您的参考点。如果jQuery(this)被删除,那么什么也不会发生(很明显)。

我可以建议您在触发 ajax 并在发送之前显示它并使用完整功能隐藏它之前隐藏一个元素,而不是用处理覆盖整个 html。

于 2013-08-15T21:56:55.087 回答