Latest web development tutorials

jQuery post () method

jQuery AJAX Methods jQuery AJAX Methods

Example 1

Using an HTTP POST request to load data from the server:

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

try it"

Example 2

Using AJAX POST request to change the text <div> element:

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

try it"

Definition and Usage

$ .post () Method uses an HTTP POST request to load data from the server.


grammar

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

参数 描述
URL 必需。规定将请求发送到哪个 URL。
data 可选。规定连同请求发送到服务器的数据。
function(data,status,xhr) 可选。规定当请求成功时运行的函数。
额外的参数:
  • data - 包含来自请求的结果数据
  • status - 包含请求的状态("success"、"notmodified"、"error"、"timeout"、"parsererror")
  • xhr - 包含 XMLHttpRequest 对象
dataType 可选。规定预期的服务器响应的数据类型。
默认地,jQuery 会智能判断。
可能的类型:
  • "xml" - 一个 XML 文档
  • "html" - HTML 作为纯文本
  • "text" - 纯文本字符串
  • "script" - 以 JavaScript 运行响应,并以纯文本返回
  • "json" - 以 JSON 运行响应,并以 JavaScript 对象返回
  • "jsonp" - 使用 JSONP 加载一个 JSON 块,将添加一个 "?callback=?" 到 URL 来规定回调


jQuery AJAX Methods jQuery AJAX Methods