Home >>jQuery Tutorial >jQuery $.post() Method

jQuery $.post() Method

jQuery $.post() Method

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

Syntax:
$(selector).post(URL,data,function(data,status,xhr),dataType)

Parameter Values

Parameter Description
URL It is a Required parameter and is used to specifies the url to send the request to
data It is an Optional parameter and is used to specifies data to send to the server along with the request
function(data,status,xhr) It is an Optional parameter and is used to specifies a function to run if the request succeeds
Additional parameters:
  • data – It is used to contains the resulting data from the request
  • status – It is used to contains the status of the request ("success", "notmodified", "error", "timeout", or "parsererror")
  • xhr - contains the XMLHttpRequest object
dataType It is an Optional parameter and is used to specifies the data type expected of the server response.
By default jQuery performs an automatic guess.
Possible types:
  • "xml" – It is used An XML document
  • "html" – It is a HTML as plain text
  • "text" – It is used to a plain text string
  • "script" – It is used to Runs the response as JavaScript, and returns it as plain text
  • "json" – It is used to Loads in a JSON block using JSONP. Will add an "?callback=?" to the URL to specify the callback
Here is an Example of jQuery post() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$.post("demo_test_post.asp",
{
name: "Phptpoint",
city: "Noida"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button class="btn1">Click me to Send an HTTP POST request</button>
</body>
</html>

Output:

No Sidebar ads