jQuery post()Method

jQuery AJAX 方法jQuery AJAX Methods

Example 1

Load data from the server using an HTTP POST request:

$("button").click(function(){ $.post("demo_test.html",function(data,status){ alert("Data: " + data + "nStatus: " + status); }); });

Try it Yourself »

Example 2

Use AJAX POST request to change the text of a <div> element:

$("input").keyup(function(){ txt=$("input").val(); $.post("demo_ajax_gethint.html",{suggest:txt},function(result){ $("span").html(result); }); });

Try it Yourself »

Definition and Usage

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


Syntax

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

Parameters Description
URL Required. Specifies which URL to send the request to.
data Optional. Specifies data to send to the server along with the request.
function(data,status,xhr) Optional. Specifies the function to run when the request succeeds.
Additional parameters:
  • data- Contains the result data from the request
  • status- Contains the status of the request ("success", "notmodified", "error", "timeout", "parsererror")
  • xhr- Contains the XMLHttpRequest object
dataType Optional. Specifies the expected data type of the server response.
By default, jQuery performs intelligent guessing.
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 it as a JavaScript object
  • "jsonp" - Loads a JSON block using JSONP, adding a "?callback=?" to the URL to specify the callback


jQuery AJAX 方法jQuery AJAX Methods

Other Extensions