jQuery get() example

jQuery get() function is used to load data from the server using an HTTP GET request.

Description

Load data from the server using an HTTP GET request.

Syntax

jQuery.get( url [, data ] [, success ] [, dataType ] )
  • url - A string containing the URL to which the request is sent.
  • data - A plain object or string that is sent to the server with the request.
  • success - A callback function that is executed if the request succeeds. Required if dataType is provided, but you can use null or jQuery.noop as a placeholder.
  • dataType - The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

Example

The following example uses the $.get() method to retrieve data from a file on the server:
<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                $.get("https://www.w3schools.com/jquery/demo_test.asp", function(data, status) {
                    console.log("Data: " + data + "\nStatus: " + status);
                    $('#output').html(data);
                });
            });
        });
    </script>
</head>

<body>

    <button>Send an HTTP GET request to a page and get the result back</button>
    <div id="output"></div>

</body>

</html>
Try it Yourself - Copy paste code in Online HTML Editor to see the result

References

Comments