Open In App

jQuery ajaxSetup() Method

The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests.

Syntax:



$.ajaxSetup( {name:value, name:value, ... } )

Parameters:

Example 1:This example uses ajaxSetup() method to call data from other files.



geeks1_data.txt: This text file is called within an HTML file.

Welcome to GeeksforGeeks

gfg.html




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery ajaxSetup() Method</title>
 
    <script src=
    </script>
 
    <script>
        $(document).ready(function () {
            $("li:parent").css("background-color", "green");
        });
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 id="geeks1" style="color:green">
        GeeksForGeeks
    </h1>
    <h2 id="geeks2">
        jQuery ajaxSetup() Method
    </h2>
    <h3></h3>
 
    <button>Click</button>
 
    <!-- Script to use ajaxSetup() method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $.ajaxSetup({
                    url: "geeks1_data.txt",
                    success: function (result) {
                        $("h3").html(result);
                    }
                });
                $.ajax();
            });
        });
    </script>
</body>
 
</html>

Output:

Example 2: This example illustrates ajaxSetup() method.




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery ajaxSetup() Method</title>
 
    <script src=
    </script>
 
    <script>
        $(document).ready(function () {
            $("li:parent").css("background-color", "green");
        });
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 id="geeks1" style="color:green">
        GeeksForGeeks
    </h1>
    <h2 id="geeks2">
        jQuery ajaxSetup() Method
    </h2>
 
    <button>Click</button>
 
    <!-- Script to use jQuery ajaxSetup() Method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $.ajaxSetup({
                    url: "geek2_dat.txt", error: function (xhr) {
                        alert("Error: " + xhr.status + " " + xhr.statusText);
                    }
                });
                $.ajax();
            });
        });
    </script>
</body>
 
</html>

Output:

other files


Article Tags :