用juery的ajax方法調(diào)用aspx.cs頁面中的webmethod方法示例
更新時間:2013年07月10日 17:32:11 作者:
juery的ajax調(diào)用aspx.cs頁面中的webmethod方法:首先在 aspx.cs文件里建一個公開的靜態(tài)方法,然后加上WebMethod屬性,具體實現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助
首先在 aspx.cs文件里建一個公開的靜態(tài)方法,然后加上WebMethod屬性。
如:
[WebMethod]
public static string GetUserName()
{
//......
}
如果要在這個方法里操作session,那還得將WebMethod的EnableSession 屬性設(shè)為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:發(fā)送信息至服務(wù)器時內(nèi)容編碼類型。我們這里一定要用 application/json 。
url:請求的服務(wù)器端處理程序的路徑,格式為"文件名(含后綴)/方法名"
data:參 數(shù)列表。注意,這里的參數(shù)一定要是json格式的字符串,記住是字符串格式,如:"{aa:11,bb:22,cc:33 , ...}"。如果你寫的不是字符串,那jquery會把它實序列化成字符串,那么在服務(wù)器端接受到的就不是json格式了,且不能為空,即使沒有參數(shù)也要 寫成"{}",如上例。
很多人不成功,原因就在這里。
dataType:服務(wù)器返回的數(shù)據(jù)類型。必須是json,其他的都無效。因為 webservice 是一json格式返回數(shù)據(jù)的,其形式為:{"d":"......."}。
success:請求成功后的回調(diào)函數(shù)。你 可以在這里對返回的數(shù)據(jù)做任意處理。
下面給個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("執(zhí)行Test方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test1()
{
Response.Write("執(zhí)行Test1方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test2()
{
Response.Write("執(zhí)行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="調(diào)用Test" onclick="CallMethod('Test')"/><input type="button" value="調(diào)用Test1"
onclick="CallMethod('Test1')"/><input type="button" value="調(diào)用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執(zhí)行方法Test返回結(jié)果\n\n\n"+msg);},
error: function(){alert('出錯了');}
}
);
})
</script>
</body>
</html>
如:
[WebMethod]
public static string GetUserName()
{
//......
}
如果要在這個方法里操作session,那還得將WebMethod的EnableSession 屬性設(shè)為true 。即:
[WebMethod(EnableSession = true)]//或[WebMethod(true)]
public static string GetUserName()
{
//......
}
然后我們就寫ajax程序來訪問這個程序,我們就用jQuery吧。
復(fù)制代碼 代碼如下:
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebForm2.aspx/GetUserName",
data: "{}",
dataType: "json",
success: function(){.......}
});
type:請求的類型,這里必須用post 。WebMethod方法只接受post類型的請求。
contentType:發(fā)送信息至服務(wù)器時內(nèi)容編碼類型。我們這里一定要用 application/json 。
url:請求的服務(wù)器端處理程序的路徑,格式為"文件名(含后綴)/方法名"
data:參 數(shù)列表。注意,這里的參數(shù)一定要是json格式的字符串,記住是字符串格式,如:"{aa:11,bb:22,cc:33 , ...}"。如果你寫的不是字符串,那jquery會把它實序列化成字符串,那么在服務(wù)器端接受到的就不是json格式了,且不能為空,即使沒有參數(shù)也要 寫成"{}",如上例。
很多人不成功,原因就在這里。
dataType:服務(wù)器返回的數(shù)據(jù)類型。必須是json,其他的都無效。因為 webservice 是一json格式返回數(shù)據(jù)的,其形式為:{"d":"......."}。
success:請求成功后的回調(diào)函數(shù)。你 可以在這里對返回的數(shù)據(jù)做任意處理。
下面給個ajax請求自身頁面的例子給你測試。。。
test.aspx
XML/HTML code
復(fù)制代碼 代碼如下:
<%@ 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("執(zhí)行Test方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test1()
{
Response.Write("執(zhí)行Test1方法"+DateTime.Now);
Response.End();//停止其他輸出
}
public void Test2()
{
Response.Write("執(zhí)行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="調(diào)用Test" onclick="CallMethod('Test')"/><input type="button" value="調(diào)用Test1"
onclick="CallMethod('Test1')"/><input type="button" value="調(diào)用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執(zhí)行方法Test返回結(jié)果\n\n\n"+msg);},
error: function(){alert('出錯了');}
}
);
})
</script>
</body>
</html>
相關(guān)文章
基于iframe實現(xiàn)ajax跨域請求 獲取網(wǎng)頁中ajax數(shù)據(jù)
這篇文章主要介紹了基于iframe實現(xiàn)ajax跨域請求,并獲取網(wǎng)頁中ajax數(shù)據(jù),如何利用網(wǎng)頁ajax請求暴露出來的接口去抓取網(wǎng)頁數(shù)據(jù)?需要的朋友可以參考下2016-01-01使用AJAX異步通信技術(shù)實現(xiàn)搜索聯(lián)想和自動補(bǔ)全示例
這篇文章主要介紹了使用AJAX異步通信技術(shù)實現(xiàn)搜索聯(lián)想和自動補(bǔ)全示例,AJAX是前后臺交互的能? 也就是我們客戶端給服務(wù)端發(fā)送消息的?具,以及接受響應(yīng)的?具,需要的朋友可以參考下2023-05-05