jQuery | ajaxSend() Method

The ajaxSend() method in jQuery is used to specify the function to be run when an AJAX request is about to send.

Syntax:

$(document).ajaxSend( function(event, xhr, options) )

Parameters: This method accepts single parameter function which is mandatory. The function accepts three parameters as mentioned above and described below:



  • event: It holds the event object.
  • xhr: It holds the XMLHttpRequest object.
  • options: It holds the used options in AJAX request.

The demo.txt file stored on server and it will load after clicking the change content button.
demo.txt:

This is GFG.

Example 1: This example changes the content of <p> element, by taking the data from server. When the AJAX request is ready to send, the page says AJAX request is about to send.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <script src
        </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;">
                GeeksforGeeks
            </h1
              
            <p id = "paragraph" style= "font-size: 20px;">
                A computer science portal for geeks
            </p
        </div>
          
        <button>
            Change Content
        </button
    </body
</html>                    

chevron_right


Output:

  • Before click on the button:
  • After click on the button:

Example 2: This example changes the content of <h1> element, by taking the data from server. When the AJAX request is ready to send, the page says AJAX request is about to send.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <script src
        </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;">
                GeeksforGeeks
            </h1
              
            <p style= "font-size: 20px;">
                A computer science portal for geeks
            </p
        </div
          
        <button>
            Change Content
        </button
    </body
</html>                    

chevron_right


Output:

  • Before click on the button:
  • After click on the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.