ajaxSend()方法在jQuery中用于指定当AJAX请求即将发送。
语法如下:
$(document).ajaxSend( function(event, xhr, options) )
参数:该方法接受强制性的单参数功能。该函数接受上述和以下所述的三个参数:
- event:它保存事件对象。
- xhr:它包含XMLHttpRequest对象。
- options:它在AJAX请求中保留使用的选项。
存储在服务器上的demo.txt文件, 单击该文件后将加载
改变内容
按钮。
demo.txt:
This is GFG.
范例1:本示例通过从服务器获取数据来更改<p>元素的内容。当AJAX请求准备好发送时, 页面会显示AJAX请求即将发送.
<!DOCTYPE html>
< html >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
<!-- Script to use ajaxSend() method -->
< script >
$(document).ready(function() {
$(document).ajaxSend(function() {
alert("AJAX request is about to send");
});
$("button").click(function() {
$("#paragraph").load("demo.txt");
});
});
</ script >
</ head >
< body style = "text-align:center;" >
< div id = "div_content" >
< h1 style = "color: green;" >
lsbin
</ h1 >
< p id = "paragraph" style = "font-size: 20px;" >
A computer science portal for geeks
</ p >
</ div >
< button >
Change Content
</ button >
</ body >
</ html >
输出如下:
在单击按钮之前:

单击按钮后:


范例2:本示例通过从服务器获取数据来更改<h1>元素的内容。当AJAX请求准备好发送时, 页面会显示AJAX请求即将发送.
<!DOCTYPE html>
< html >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
<!-- Script to use ajaxSend() method -->
< script >
$(document).ready(function() {
$(document).ajaxSend(function() {
alert("AJAX request is about to send");
});
$("button").click(function() {
$("#heading").load("demo.txt");
});
});
</ script >
</ head >
< body style = "text-align:center;" >
< div id = "div_content" >
< h1 id = "heading" style = "color: green;" >
lsbin
</ h1 >
< p style = "font-size: 20px;" >
A computer science portal for geeks
</ p >
</ div >
< button >
Change Content
</ button >
</ body >
</ html >
输出如下:
在单击按钮之前:

单击按钮后:

