jQuery ajax:事件发生前

I've seen some coworkers use this in rails

    $('[data-once=true]').live('ajax:before', function(request, e) {

});

However I'm trying to use it with jquery and it doesnt work, does this event comes with the rails jquery adapter?

This 'ajax:before' isn't a useful event for .live() to handle, unless you've defined a custom event with that name.

I'm not a rails dev, so I'm not sure how rails changes this equation, but this is how I'd attach an function to the AJAX before in plain ole' JS:

$.ajax({
   beforeSend: function(){

     alert("Before");

   },
   complete: function(){

    alert("after");

   }
 });

Note that these events are fired for all AJAX requests thereafter. You're essentially subscribing to the AJAX object.

See the jQuery AJAX Events Documentation for more info.

Yes, ajax:before is a jQuery event that rails adds/triggers, though in the latest version it's not ajax:beforeSend. In jQuery though (ajax:before was around before the JS framework agnostic changes to rails), you can just attach to this globally, using the ajaxSend global event, like this:

$(document).bind('ajaxSend', function(e, request, options) {
  $('[data-once=true]').something();
});