Open In App

jQuery $.ajaxPrefilter() Method

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Jquery $.ajaxPrefilter() method is a powerful jquery method that allows us to modify the settings, option object, URL, and data object of AJAX jquery request before sending a request to the server. If you know the syntax of the AJAX jquery request, you will find that it contains options, objects, and methods that will be performed during the execution of the request. The $.ajaxPrefilter() method can help you modify these parameters of the AJAX jquery request. 

Syntax: Here is the syntax of the AJAX Jquery Request:

$.ajax({
url: "/https/www.geeksforgeeks.org/path",
type: "POST/GET",
async: true/false,
success: function(response) {
// task ...
}
error:function(error){
// error handling.
}
})

The $.ajaxPrefilter() method can help you modify the value of URL, type, success, etc before the request has been sent to the server. This method is helpful in providing additional data objects and headers for the request. 

Syntax: Here is the syntax of the  $.ajaxPrefilter() method:

$.ajaxPrefilter([ data_types], handler);

Parameters: 

  • Datatypes: It is an optional parameter. You can specify the data types for the option objects as a string or array of strings. If it is not specified then all types of data types will be allowed.
  • Handler: It is a function and will automatically be called for each request. The following are the parameters for this function:
    • Options: It is an object that contains URL, type, and AJAX request settings.
    • OriginalOptions: It is an object of original and final options after the modification has been done. 
    • JqXHR: This parameter option represents the Jquery XHR object.

Let's understand the method with the help of examples and implement the method using Jquery and PHP.

Approach: In this approach, we have created two PHP files ( Let's say Index.php & gfg.php ). The index.php file contains the implementation of the AJAX jquery request as well as the $.ajaxPrefilter() method. The AJAX jquery method sends the data object to gfg.php and then gfg.php sends the received data response to the index.php file. The index.php file displays the received response. 

Example 1: This example illustrates the use of the $.ajaxPrefilter() method to add two more data values with the data object of the AJAX jquery request.

  • Index.php
PHP
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
    </script>
</head>

<body>
    <h2 style="color: green">
        GeeksforGeeks
    </h2>
    <h3>
        $.ajaxPrefilter() Jquery Method
    </h3>

    <button onclick="sendReq()">
        Send AJAX Request
    </button>
    <br><br>
    <div id="resbox">Display Response!</div>

    <script>
        $(document).ready(function () {
            $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
                options.data = $.param($.extend(originalOptions.data, {
                    salary: '20,000',
                    phone: '0123456789'
                }));
            });
        });
        const sendReq = () => {
            $.ajax({
                type: "POST",
                url: 'gfg.php',
                data: {
                    name: "Geek",
                    id: "101"
                },
                success: function (response) {
                    document.getElementById('resbox').innerHTML = response;
                }
            });
        }
    </script>
</body>

</html>
  • gfg.php ( server ):
PHP
<?php
echo "User : ". $_POST['name'] . "\n id : " . $_POST['id']. 
" Phone : " . $_POST['phone'] . " Salary : " , $_POST['salary'];
?>

Output: In the above PHP, the $.ajaxPrefilter() method adds salary and phone key-value pairs to the data object of the AJAX jquery request that already contains name and id key-value pairs. The success method displays the response from the server page.


Example 2:  In the below example, we have changed the object options type, URL, and data using the $.ajaxPrefilter() method. In the AJAX request the type, URL, and data have the following values:

type:"POST",

url:"gfg.php",

data:{ name: "Geek", id: "101"}

but, these values have been changed by the $.ajaxPrefilter() method. Now the request will send to redirect.php through the "GET" method and the response from the redirect.php will be displayed.

  • Index.php:
PHP
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
    </script>
</head>

<body>
    <h2 style="color: green">
        GeeksforGeeks
    </h2>
    <h3>
        $.ajaxPrefilter() Jquery Method
    </h3>

    <button onclick="sendReq()">
        Send AJAX Request
    </button>
    <br><br>
    <div id="resbox">
        Display Response!
    </div>

    <script>
        $(document).ready(function () {
            $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
                options.url = "redirect.php";
                options.type = "GET";
                options.data = null;
            });
        });
        const sendReq = () => {
            $.ajax({
                type: "POST",
                url: 'gfg.php',
                data: {
                    name: "Geek",
                    id: "101"
                },
                success: function (response) {
                    document.getElementById('resbox').innerHTML = response;
                }
            });
        }
    </script>
</body>

</html>
  • redirect.php:
PHP
<?php 
echo "<h2 style='color:red'>Redirect.php</h2>";
?>

Output:



Next Article

Similar Reads