jquery中AJAX請求 $.post方法的使用
使用jQuery的$.post方法可以以POST形式向服務(wù)器發(fā)起AJAX請求。$.post方法是jQuery的實(shí)用工具方法。
post和get發(fā)送方式的特點(diǎn), GET 方法提交數(shù)據(jù)不安全,數(shù)據(jù)置于請求行,客戶端地址欄可見; GET 方法提交的數(shù)據(jù)大小限制在255 個字符之內(nèi)。POST方法提交的數(shù)據(jù)置于消息主體內(nèi),客戶端不可見, POST 方法提交的數(shù)據(jù)大小沒有限制。
$.post方法語法
$.post(url,parameters,callback) |
|
參數(shù) |
|
url |
(字符串)服務(wù)器端資源地址。 |
parameter |
(對象)需要傳遞到服務(wù)器端的參數(shù)。 參數(shù)形式為“鍵/值”。 |
callback |
(函數(shù))在請求完成時被調(diào)用。該函數(shù)參數(shù)依次為響應(yīng)體和狀態(tài)。 |
返回值 |
XHR實(shí)例 |
看個簡單的例子
客戶端代碼:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $().ready(function () { $('#selectNum').change(function () { var idValue = $(this).val(); //采用POST方式調(diào)用服務(wù) $.post('Server.aspx', { id: idValue }, function (text, status) { alert(text); }); }) }) </script> </head> <body> <select id="selectNum"> <option value="0">--Select--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </body> </html>
服務(wù)端主要代碼:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request["id"] != null && !string.IsNullOrEmpty(Request["id"].ToString())) { Response.Write( GetData(Request["id"].ToString())); } } } protected string GetData(string id) { string str = string.Empty; switch (id) { case "1": str += "This is Number 1"; break; case "2": str += "This is Number 2"; break; case "3": str += "This is Number 3"; break; default: str += "Warning Other Number!"; break; } return str; }
運(yùn)行程序,結(jié)果如圖:
用httpwatcher攔截請求信息,當(dāng)下拉框中選擇數(shù)字時,可以截取到如下請求信息。
使用$.post方法時的截圖:
通過上圖我們可以看到在POST Data里面有參數(shù),說明這是一次POST請求。
在服務(wù)器端狀態(tài)有改變,或者是修改更新某些數(shù)據(jù)時多用POST請求。
相關(guān)文章
如何利用jQuery post傳遞含特殊字符的數(shù)據(jù)
在jquery中,解決數(shù)據(jù)傳遞處理的方法我們通常利用$.ajax或$.post,但是這里這里通常不能傳遞特殊字符,比如說:“<”,本文就幫大家解決如何傳遞這種含特殊字符的數(shù)據(jù),感興趣的朋友一起看下吧2015-10-10ajax 實(shí)現(xiàn)微信網(wǎng)頁授權(quán)登錄的方法
這篇文章主要介紹了ajax 實(shí)現(xiàn)微信網(wǎng)頁授權(quán)登錄的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)...2007-09-09滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
滾動下拉到頁面底部加載數(shù)據(jù)是很多瀑布流網(wǎng)站的做法,那來看看配合jsonp是如何實(shí)現(xiàn)的吧,小菜總結(jié)記錄之用特在此與大家一起分享,感興趣的朋友可以參考下哈2013-05-05