?
This document uses PHP Chinese website manual Release
通過遠(yuǎn)程 HTTP POST 請(qǐng)求載入信息。
這是一個(gè)簡單的 POST 請(qǐng)求功能以取代復(fù)雜 $.ajax 。請(qǐng)求成功時(shí)可調(diào)用回調(diào)函數(shù)。如果需要在出錯(cuò)時(shí)執(zhí)行函數(shù),請(qǐng)使用 $.ajax。
jQuery 1.12 中 jQuery.post 和 jQuery.get 支持對(duì)象參數(shù),這樣一來好處還比較多,比如設(shè)置回調(diào)函數(shù)的context,或者跨域 post 時(shí)可以withCredential: true。用法可以參考示例9。
url:發(fā)送請(qǐng)求地址。
data:待發(fā)送 Key/value 參數(shù)。
callback:發(fā)送成功時(shí)回調(diào)函數(shù)。
type:返回內(nèi)容格式,xml, html, script, json, text, _default。
請(qǐng)求 test.php 網(wǎng)頁,忽略返回值:
$.post("test.php");
請(qǐng)求 test.php 頁面,并一起發(fā)送一些額外的數(shù)據(jù)(同時(shí)仍然忽略返回值):
$.post("test.php", { name: "John", time: "2pm" } );
向服務(wù)器傳遞數(shù)據(jù)數(shù)組(同時(shí)仍然忽略返回值):
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
使用 ajax 請(qǐng)求發(fā)送表單數(shù)據(jù):
$.post("test.php", $("#testform").serialize());
輸出來自請(qǐng)求頁面 test.php 的結(jié)果(HTML 或 XML,取決于所返回的內(nèi)容):
$.post("test.php", function(data){ alert("Data Loaded: " + data); });
向頁面 test.php 發(fā)送數(shù)據(jù),并輸出結(jié)果(HTML 或 XML,取決于所返回的內(nèi)容):
$.post("test.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });
獲得 test.php 頁面的內(nèi)容,并存儲(chǔ)為 XMLHttpResponse 對(duì)象,并通過 process() 這個(gè) JavaScript 函數(shù)進(jìn)行處理:
$.post("test.php", { name: "John", time: "2pm" }, function(data){ process(data); }, "xml");
獲得 test.php 頁面返回的 json 格式的內(nèi)容::
$.post("test.php", { "func": "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); // 2pm }, "json");
jQuery 1.12 中 jQuery.post支持對(duì)象參數(shù),具體的參數(shù)可以參考 $.ajax():
jQuery.post({ url: “/example” });