用juery的ajax方法調用aspx.cs頁面中的webmethod方法樣本

來源:互聯網
上載者:User
juery的ajax調用aspx.cs頁面中的webmethod方法:首先在 aspx.cs檔案裡建一個公開的靜態方法,然後加上WebMethod屬性,具體實現如下,感興趣的朋友可以參考下哈,希望對大家有所協助 

首先在 aspx.cs檔案裡建一個公開的靜態方法,然後加上WebMethod屬性。
如:
[WebMethod]
public static string GetUserName()
{
//......
}
如果要在這個方法裡操作session,那還得將WebMethod的EnableSession 屬性設為true 。即:
[WebMethod(EnableSession = true)]//或[WebMethod(true)]
public static string GetUserName()
{
//......
}
然後我們就寫ajax程式來訪問這個程式,我們就用jQuery吧。

複製代碼 代碼如下:
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebForm2.aspx/GetUserName",
data: "{}",
dataType: "json",
success: function(){.......}
});


type:請求的類型,這裡必須用post 。WebMethod方法只接受post類型的請求。
contentType:發送資訊至伺服器時內容編碼類別型。我們這裡一定要用 application/json 。
url:請求的伺服器端處理常式的路徑,格式為"檔案名稱(含尾碼)/方法名"
data: 參 數列表。注意,這裡的參數一定要是json格式的字串,記住是字串格式,如:"{aa:11,bb:22,cc:33 , ...}"。如果你寫的不是字串,那jquery會把它實序列化成字串,那麼在伺服器端接受到的就不是json格式了,且不可為空,即使沒有參數也要 寫成"{}",如上例。
很多人不成功,原因就在這裡。
dataType:伺服器返回的資料類型。必須是json,其他的都無效。因為 webservice 是一json格式返回資料的,其形式為:{"d":"......."}。
success:請求成功後的回呼函數。你 可以在這裡對返回的資料做任意處理。
下面給個ajax請求自身頁面的例子給你測試。。。
test.aspx
XML/HTML code

複製代碼 代碼如下:


<%@ Page language="C#"%>
<script runat="server">
protected void Page_Load(object sender,EventArgs e){
Response.Charset="gb2312";
if(Request.Form["method"]=="Test")Test();
else if(Request.Form["method"]=="Test1")Test1();
else if(Request.Form["method"]=="Test2")Test2();
Response.Write("一般請求<br/>");
}
public void Test()
{
Response.Write("執行Test方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test1()
{
Response.Write("執行Test1方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test2()
{
Response.Write("執行Test2方法"+DateTime.Now);
Response.End();//停止其他輸出
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<input type="button" value="調用Test" onclick="CallMethod('Test')"/><input type="button" value="調用Test1"
onclick="CallMethod('Test1')"/><input type="button" value="調用Test2" onclick="CallMethod('Test2')"/>
<script type="text/javascript">
function CallMethod(method){
$.ajax(
{
type: "POST",
url: "test.aspx",
data:{method:method},
success:function(msg){alert(msg);},
error: function(){alert('出錯了');}
}
)
}
$(document).ready(function(){
$.ajax(
{
type: "POST",
url: "test.aspx",
data:{method:"Test"},
success:function(msg){alert("$(document).ready執行方法Test返回結果nnn"+msg);},
error: function(){alert('出錯了');}
}
);
})
</script>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.