jQuery post() Method

jQuery post() method used to loads data from the server using a HTTP POST request.

Syntax

Here is a syntax for the post() method

$.post( url, data, function(data, textStatus, jqXHR), dataType );
Parameter Type Description
URL String Required. Specifies the URL to send the request to.
data Object
String
Optional. Specifies data to sent to the server with the request.
function(data, textStatus, jqXHR) Function Optional. Specifies callback function that is run if the request succeeds.
Additional parameters
  • data - contains the result data from the request
  • textStatus - contains the status of the request
  • jqXHR - contains the XMLHttpRequest object
dataType String Optional. Specifies the data type expected from the server response.
Default: Automatic Guess (xml, html, text, script, json, jsonp)

Example

get_method.php
<?php
if( $_REQUEST["name"] )
{
   $name = $_REQUEST["name"];
   echo "Welcome ". $name;
}
?>

Here, in this example post() method call when click on button.

<!DOCTYPE html>
<html>
<head>
  <title>jQuery post() method</title>
  <script src="jquery-latest.min.js"></script>
  <script>
    $(document).ready(function(){
      $("button").click(function(){
        $.post("get_method.php", {name: "Paul"}, function(data){
          $("#msg").html(data);
        });
      });
    });
  </script>
</head>
<body>
  <div id="msg">This text replace with new contains</div>
  <br />
  <button>Click here to call post() method</button>
</body>
</html>

Run it...   »

More Examples

Request the "get_method.php", but ignore the return results:

$.post( "get_method.php" );

Request the "get_method.php" URL and send some additional data along with the request:

$.post( "get_method.php", { name: "Paul", city: "London" } );

Request the "get_method.php" and pass arrays of data to the server:

$.post( "get_method.php", { "hobbies[]": ["Cooking", Gardening", "Knitting"] } );

Request the "get_method.php" and alert the result of the request:

$.post( "get_method.php", function(data){
  alert("Data Received: " + data);
});