.ajaxSend()

Description: Attach a function to be executed before an Ajax request is sent. This is an Ajax Event.version added: 1.0
描述:绑定一个在ajax请求被发送前执行的函数。这个一个ajax事件。起始版本1.0

.ajaxSend( handler(event, jqXHR, ajaxOptions) )
handler(event, jqXHR, ajaxOptions)The function to be invoked.被调用的函数。

Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend event. Any and all handlers that have been registered with the .ajaxSend() method are executed at this time.
每当一个ajax请求将要被发送的时候,jQuery将会触发ajaxSend事件。这个时候所有注册在.ajaxSend()的方法就会被执行。

To observe this method in action, we can set up a basic Ajax load request:
为了观察该方法的实际运行,我们可以建立一个基本的ajax加载请求:

<div class="trigger">Trigger</div>
<div class="result"></div>
<div class="log"></div>
We can attach our event handler to any element:
我们可以将事件处理器绑定到任何的元素。

$('.log').ajaxSend(function() {
  $(this).text('Triggered ajaxSend handler.');
});
Now, we can make an Ajax request using any jQuery method:
现在我们可以使用任何一个jQuery Ajax方法建立一个ajax请求:


$('.trigger').click(function() {
  $('.result').load('ajax/test.html');
});
When the user clicks the element with class trigger and the Ajax request is about to begin, the log message is displayed.
当用户点击了拥有trigger class的元素并且ajax请求发送前log信息将会被显示。

Note: Because .ajaxSend() is implemented as a method of jQuery instances, we can use the this keyword as we do here to refer to the selected elements within the callback function.
注意:因为.ajaxSend()是作为一个jQuery对象实例方法去执行的,所以我们可以在回调函数中使用this关键字表示当前被选择的元素。

All ajaxSend handlers are invoked, regardless of what Ajax request is to be sent. If we must differentiate between the requests, we can use the parameters passed to the handler. Each time an ajaxSend handler is executed, it is passed the event object, the jqXHR object (in version 1.4, XMLHttpRequestobject), and the settings object that was used in the creation of the Ajax request. For example, we can restrict our callback to only handling events dealing with a particular URL:
无论是哪种ajax请求发送前,所有的ajaxSend处理器都将会被执行。如果非得区分请求,我们可以根据传递给处理器的参数加于区分。当ajaxSend处理器执行时,event对象、jqXHR对象(在1.4版本是XMLHttpRequest对象)、创建该请求的设置对象,这3个对象都会被传递给处理器。如果是因为javascript引起异常导致请求失败时,该异常对象将会被当做第四个参数传递处理器。例如,我们可以限制我们的回调函数只处理特定的URL。

$('.log').ajaxSend(function(e, jqxhr, settings) {
  if (settings.url == 'ajax/test.html') {
    $(this).text('Triggered ajaxSend handler.');
  }
});
Example:
Show a message before an Ajax request is sent.
例子:在ajax请求发送前显示一段信息。
$("#msg").ajaxSend(function(evt, request, settings){
        $(this).append("<li>Starting request at " + settings.url + "</li>");
      });

你可能感兴趣的:(jquery)