Cover Image for jQuery ajax() method
131 views

jQuery ajax() method

The jQuery ajax() method is a versatile and powerful function used to perform AJAX (Asynchronous JavaScript and XML) requests to a server. It provides a way to send and receive data from the server asynchronously without requiring a page reload. The ajax() method is one of the fundamental functions for making AJAX requests with jQuery.

Here’s the basic syntax of the ajax() method:

$.ajax({
  url: "your_url",
  type: "GET/POST/PUT/DELETE", // HTTP method to be used for the request
  data: {}, // Data to be sent to the server (optional)
  dataType: "json/xml/html/text", // Expected data type of the response (optional)
  success: function(response) {
    // Callback function executed when the request is successful
  },
  error: function(jqXHR, textStatus, errorThrown) {
    // Callback function executed when the request encounters an error
  },
  complete: function(jqXHR, textStatus) {
    // Callback function executed when the request is complete, regardless of success or error
  }
});

Parameters:

  • url: The URL to which the request is sent.
  • type: The HTTP method to be used for the request (e.g., “GET”, “POST”, “PUT”, “DELETE”).
  • data: Data to be sent to the server in the request body (optional). It can be an object, a query string, or a serialized form data.
  • dataType: The data type of the expected response (optional). It can be “json”, “xml”, “html”, or “text”.
  • success: A callback function that is executed if the request is successful. The function receives the response data as a parameter.
  • error: A callback function that is executed if the request encounters an error. It receives three parameters: jqXHR, textStatus, and errorThrown.
  • complete: A callback function that is executed when the request is complete, regardless of whether it was successful or encountered an error.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery ajax() Method Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button id="load-data">Load Data</button>
    <div id="output"></div>

    <script>
        $(document).ready(function() {
            $('#load-data').click(function() {
                // Send a GET request to retrieve data from a JSON file
                $.ajax({
                    url: 'data.json',
                    type: 'GET',
                    dataType: 'json',
                    success: function(data) {
                        // Display the data in the output div
                        $('#output').text(JSON.stringify(data));
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        // Display an error message if the request fails
                        $('#output').text('Error: ' + textStatus);
                    }
                });
            });
        });
    </script>
</body>
</html>

In this example, we have a button with the ID “load-data” and a div with the ID “output.” When the button is clicked, the ajax() method is used to make a GET request to the “data.json” file, which contains JSON data. Upon successful retrieval, the JSON data is displayed in the “output” div. If there is an error, an error message is shown instead.

The ajax() method is a powerful tool for working with AJAX requests, and it offers a wide range of options and flexibility for handling different scenarios in web development. It is commonly used to interact with APIs, fetch data from servers, submit form data, and perform other asynchronous tasks in web applications.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS