|
$.post(url,parameters,callback) |
|
|
參數(shù) |
|
|
url |
(字符串)服務(wù)器端資源地址。 |
|
parameter |
(對象)需要傳遞到服務(wù)器端的參數(shù)。 參數(shù)形式為“鍵/值”。 |
|
callback |
(函數(shù))在請求完成時(shí)被調(diào)用。該函數(shù)參數(shù)依次為響應(yīng)體和狀態(tài)。 |
|
返回值 |
XHR實(shí)例 |
看個(gè)簡單的例子
客戶端代碼:
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ù)字時(shí),可以截取到如下請求信息。
使用$.post方法時(shí)的截圖:

通過上圖我們可以看到在POST Data里面有參數(shù),說明這是一次POST請求。
在服務(wù)器端狀態(tài)有改變,或者是修改更新某些數(shù)據(jù)時(shí)多用POST請求。
標(biāo)簽:紅河 荊州 辛集 揭陽 佛山 咸寧 商洛 股票
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jquery中AJAX請求 $.post方法的使用》,本文關(guān)鍵詞 jquery,中,AJAX,請求,.post,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。