多種方式實現(xiàn)JS調(diào)用后臺方法進行數(shù)據(jù)交互
更新時間:2013年08月20日 16:05:06 作者:
幾種典型常用的方法如利用控件的AutopostBack屬性、Button提交表單等等,下面為大家分享下JS調(diào)用后臺方法進行數(shù)據(jù)交互示例
項目開發(fā)過程中很多地方都需要前端和后臺的數(shù)據(jù)交互,幾種典型常用的方法如利用控件的AutopostBack屬性、Button提交表單等等。但這些都是有條件的,AutoPostBack具有實時性但會刷新頁面,Button提交表單不能實現(xiàn)數(shù)據(jù)交互的實時性。當然說到前臺與后臺的數(shù)據(jù)交互更不能漏掉ajax,ajax實現(xiàn)前臺與后臺數(shù)據(jù)的異步交互,并且保證實時的、局部刷新。但有些數(shù)據(jù)不需要異步交互,例如當交互的數(shù)據(jù)是下一步執(zhí)行的條件時,就必須要等到數(shù)據(jù)前臺與后臺數(shù)據(jù)交互完成后才能繼續(xù)執(zhí)行程序。所以對于掌握js與后臺數(shù)據(jù)交互的方法還是很有必要的。
方法一
后臺方法:
<SPAN style="COLOR: #ff0000">// 需要標識為WebMethod</SPAN>
[System.Web.Services.WebMethod]
<SPAN style="COLOR: #ff0000">// 注意,要讓前臺調(diào)用的方法,一定要是public和static的</SPAN>
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
前臺js:
<script type="text/javascript">
function btnClick(){
PageMethods.Say("you",funReady,funError);<SPAN style="COLOR: #ff6666">//注意js中調(diào)用后臺方法的方式</SPAN>
}
<SPAN style="COLOR: #ff0000">//回調(diào)函數(shù), result 就是后臺方法返回的數(shù)據(jù)</SPAN>
function funReady(result){
alert(result);
}
<SPAN style="COLOR: #ff0000">//錯誤處理函數(shù),err 就是后臺方法返回的錯誤信息</SPAN>
function funError(err){
alert("Error:" + err._message );
}
</script>
<asp:ScriptManagerID="ScriptManager1" runat="server"EnablePageMethods="true" />
<inputtype="button" onclick="btnClick()" value="test"/>
方法二
后臺方法:
protected string Say(string strCC)
{
strCC = "你好!" + strCC;
return strCC;
}
前臺js:
function Show()
{
var v = "中國";
var s = '<%=Say("'+v+'") %>'; // 你好!“+V+”
alert(s);
}<P style="MARGIN: 0in; FONT-FAMILY: Arial; COLOR: #666666; FONT-SIZE: 9pt"><input type="button" onclick="Show()" value="提交" /></P>
方法三
后臺方法:
<SPAN style="COLOR: #666666">// 需要標識為WebMethod
[System.Web.Services.WebMethod]
// </SPAN><SPAN style="COLOR: #ff0000">注意,要讓前臺調(diào)用的方法,一定要是public和static的</SPAN><SPAN style="COLOR: #666666">
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
</SPAN>
前臺js:
<SPAN style="COLOR: #666666"><script type="text/javascript">
function btnClick(){
// </SPAN><SPAN style="COLOR: #ff0000">調(diào)用頁面后臺方法,前面跟方法所需的參數(shù),接著是方法回調(diào)成功時要執(zhí)行的js函數(shù),最后一個是方法回調(diào)失敗時要執(zhí)行的js函數(shù)</SPAN><SPAN style="COLOR: #666666">
WebSerCustomer.Say("you",function(ress){//ress就是后臺方法返回的數(shù)據(jù),Say是webservice WebSerCustomer.axms頁面上的方法
alert(ress)
});
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services><asp:ServiceReference Path="~/WebSerCustomer.asmx" /></Services>//WebSerCustomer.asmx后臺webservice類的頁名稱
</asp:ScriptManager>
<input type="button" onclick="btnClick()" value="test" /></SPAN>
總結(jié)
對于方法一和方法三來說,標識System.web.Services.webmethod可以聲明一個方法可以通過客戶端js函數(shù)來調(diào)用,并且后臺方法必須聲明為public和static,正是由于要將方法聲明為static,使得這兩種方法都有局限性,即靜態(tài)方法中只允許訪問靜態(tài)成員變量。所以要想用這兩種方式調(diào)用后臺方法,后臺方法中是不能訪問非靜態(tài)成員變量的。
對于方法二來說,雖然后臺方法沒有任何限制,但是前臺調(diào)用的時候由于<%=%>是只讀的,前臺向后臺傳的參數(shù)實際上是不存在的,即從后臺中拿不到。所以方法二適合于調(diào)用后臺方法經(jīng)過處理并返回給客戶端使用,不適合于將數(shù)據(jù)傳到后臺供后臺使用。
方法一
后臺方法:
復(fù)制代碼 代碼如下:
<SPAN style="COLOR: #ff0000">// 需要標識為WebMethod</SPAN>
[System.Web.Services.WebMethod]
<SPAN style="COLOR: #ff0000">// 注意,要讓前臺調(diào)用的方法,一定要是public和static的</SPAN>
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
前臺js:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function btnClick(){
PageMethods.Say("you",funReady,funError);<SPAN style="COLOR: #ff6666">//注意js中調(diào)用后臺方法的方式</SPAN>
}
<SPAN style="COLOR: #ff0000">//回調(diào)函數(shù), result 就是后臺方法返回的數(shù)據(jù)</SPAN>
function funReady(result){
alert(result);
}
<SPAN style="COLOR: #ff0000">//錯誤處理函數(shù),err 就是后臺方法返回的錯誤信息</SPAN>
function funError(err){
alert("Error:" + err._message );
}
</script>
<asp:ScriptManagerID="ScriptManager1" runat="server"EnablePageMethods="true" />
<inputtype="button" onclick="btnClick()" value="test"/>
方法二
后臺方法:
復(fù)制代碼 代碼如下:
protected string Say(string strCC)
{
strCC = "你好!" + strCC;
return strCC;
}
前臺js:
復(fù)制代碼 代碼如下:
function Show()
{
var v = "中國";
var s = '<%=Say("'+v+'") %>'; // 你好!“+V+”
alert(s);
}<P style="MARGIN: 0in; FONT-FAMILY: Arial; COLOR: #666666; FONT-SIZE: 9pt"><input type="button" onclick="Show()" value="提交" /></P>
方法三
后臺方法:
復(fù)制代碼 代碼如下:
<SPAN style="COLOR: #666666">// 需要標識為WebMethod
[System.Web.Services.WebMethod]
// </SPAN><SPAN style="COLOR: #ff0000">注意,要讓前臺調(diào)用的方法,一定要是public和static的</SPAN><SPAN style="COLOR: #666666">
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
</SPAN>
前臺js:
復(fù)制代碼 代碼如下:
<SPAN style="COLOR: #666666"><script type="text/javascript">
function btnClick(){
// </SPAN><SPAN style="COLOR: #ff0000">調(diào)用頁面后臺方法,前面跟方法所需的參數(shù),接著是方法回調(diào)成功時要執(zhí)行的js函數(shù),最后一個是方法回調(diào)失敗時要執(zhí)行的js函數(shù)</SPAN><SPAN style="COLOR: #666666">
WebSerCustomer.Say("you",function(ress){//ress就是后臺方法返回的數(shù)據(jù),Say是webservice WebSerCustomer.axms頁面上的方法
alert(ress)
});
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services><asp:ServiceReference Path="~/WebSerCustomer.asmx" /></Services>//WebSerCustomer.asmx后臺webservice類的頁名稱
</asp:ScriptManager>
<input type="button" onclick="btnClick()" value="test" /></SPAN>
總結(jié)
對于方法一和方法三來說,標識System.web.Services.webmethod可以聲明一個方法可以通過客戶端js函數(shù)來調(diào)用,并且后臺方法必須聲明為public和static,正是由于要將方法聲明為static,使得這兩種方法都有局限性,即靜態(tài)方法中只允許訪問靜態(tài)成員變量。所以要想用這兩種方式調(diào)用后臺方法,后臺方法中是不能訪問非靜態(tài)成員變量的。
對于方法二來說,雖然后臺方法沒有任何限制,但是前臺調(diào)用的時候由于<%=%>是只讀的,前臺向后臺傳的參數(shù)實際上是不存在的,即從后臺中拿不到。所以方法二適合于調(diào)用后臺方法經(jīng)過處理并返回給客戶端使用,不適合于將數(shù)據(jù)傳到后臺供后臺使用。
相關(guān)文章
JS將數(shù)字轉(zhuǎn)換成三位逗號分隔的樣式(示例代碼)
本篇文章主要是對JS將數(shù)字轉(zhuǎn)換成三位逗號分隔的樣式(示例代碼)進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02javascript 實現(xiàn)的類似hao123的多郵箱登錄效果
javascript 實現(xiàn)的類似hao123的多郵箱登錄效果...2007-08-08JavaScript talbe表中指定位置插入一行的實現(xiàn)代碼 腳本之家修正版
用js實現(xiàn)的在table中指定的位置插入一行,先點一下表中你想插入的位置,點擊即可。2009-06-06