post() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:post

jQuery post() Method

Description

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

Syntax

$(selector).post(URL, data, function(data,status,xhr), dataType);
Parameter Require Description
URL Required. url to send the request to
data Optional. data to send to the server along with the request
function(data,status,xhr) Optional. function to run if the request succeeds Additional parameters: data - contains the resulting data from the request status - contains the status of the request ("success", "notmodified", "error", "timeout", or "parsererror") xhr - contains the XMLHttpRequest object
dataType Optional. data type of the server response.

Possible data 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.

The following code shows how to load data from the server using a HTTP POST request:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").keyup(function(){
        var txt = $("input").val();
        $.post("demo.asp", {suggest: txt}, function(result){
            $("span").html(result);
        });/*  w w w  . j av a  2 s  . c  o m*/
    });
});
</script>
</head>
<body>

First name:
<input type="text">

<p>Suggestions: <span></span></p>

</body>
</html>

Related Tutorials