jQuery.post(url, [data], [callback], [type])
概述
通過遠程 HTTP POST 請求載入信息。
這是一個簡單的 POST 請求功能以取代復雜 $.ajax 。請求成功時可調用回調函數。如果需要在出錯時執行函數,請使用 $.ajax。
參數
url,[data],[callback],[type]String,Map,Function,StringV1.0
url:發送請求地址。
data:待發送 Key/value 參數。
callback:發送成功時回調函數。
type:返回內容格式,xml, html, script, json, text, _default。
示例
1)向服務器傳遞數據數組(同時仍然忽略返回值):
jQuery 代碼:
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
2)使用 ajax 請求發送表單數據:
jQuery 代碼:
$.post("test.php", $("#testform").serialize());
3)向頁面 test.php 發送數據,并輸出結果(HTML 或 XML,取決于所返回的內容):
jQuery 代碼:
$.post("test.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });
4)獲得 test.php 頁面的內容,并存儲為 XMLHttpResponse 對象,并通過 process() 這個 JavaScript 函數進行處理:
jQuery 代碼:
$.post("test.php", { name: "John", time: "2pm" }, function(data){ process(data); }, "xml");
5)獲得 test.php 頁面返回的 json 格式的內容::
jQuery 代碼:
$.post("test.php", { "func": "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); // 2pm }, "json");
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com