jQuery post() example

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

Description

Load data from the server using an HTTP POST request.

Syntax

jQuery.post( 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 can be null in that case.
  • dataType - The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

Example

The following example uses the $.post() method to send some data along with the request:
<!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() {
                $.post("https://www.w3schools.com/jquery/demo_test_post.asp", {
                        name: "Admin",
                        city: "London"
                    },
                    function(data, status) {
                        alert("Data: " + data + "\nStatus: " + status);
                        $('#output').html(data);
                    });
            });
        });
    </script>
</head>

<body>

    <button>Send an HTTP POST 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

Reference

Comments