jQuery post()

Introduction

Load data from the server using a HTTP POST request:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/* w  w  w . ja va2 s  .  c  o m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("ajax.txt",
    {
      name: "CSS",
      city: "New York"
    },
    function(data,status){
      document.getElementById("demo").innerHTML = 
               "Data: " + data + "\nStatus: " + status;
    });
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<button>Send an HTTP POST request to 
a page and get the result back</button>
</body>
</html>

The $.post() method loads data from the server using a HTTP POST request.

$(selector).post(URL,data,function(data,status,xhr),dataType)
Parameter
Optional
Description
URL
Required.
the url to send the request to
data
Optional.
data to send to the server along with the request
function(data,status,xhr)









Optional.









sets a function to run if the request succeeds
Additional parameters:
data - the resulting data from the request
status - the status of the request
"success"
"notmodified"
"error"
"timeout"
"parsererror"
xhr - the XMLHttpRequest object
dataType








Optional.








the data type expected of the server response.
By default jQuery performs an automatic guess.
Possible types:
"xml" - An XML document
"html" - HTML as plain text
"text" - A plain text string
"script" - Runs the response as JavaScript, and returns it as plain text
"json" - Runs the response as JSON, and returns a JavaScript object
"jsonp" - Loads in a JSON block using JSONP.



PreviousNext

Related