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 関数で行っていることは、1 レベル上がり、すべてのHTML を「処理中...」に置き換えることです。jQuery(this)これにより、成功ケースに到達する前に、DOM から参照ポイントが削除されました。が削除された場合jQuery(this)、何も起こりません (明らか)。

Processingでhtml全体を上書きする代わりに、ajaxをトリガーしてbeforeSendを表示し、完全な関数で非表示にするまで、単一の要素を非表示にすることをお勧めします。

于 2013-08-15T21:56:55.087 に答える