JS中獲取數(shù)據(jù)庫中的值的方法
更新時間:2013年07月14日 15:33:16 作者:
在項目中遇到一個問題,需要在JS中讀取數(shù)據(jù)庫中的值,然后再把值返回到頁面中,解決方案如下:使用Ajax方法來實現(xiàn),需要用到ajax.dll(一個ajax技術(shù)開發(fā)的幫助類庫)。
實施過程如下:
1、引用Ajax.dll
2、在App_Code寫具體的方法,最好單獨建立一個類文件,然后寫具體方法。
public class AjaxMethod www.dbjr.com.cn
{
public AjaxMethod()
{
//
//TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetCardMoney(string cardNo,string cardPwd)
{
string mConn = IConfiguration.getParameter("connectString");
IDBOSQL.IDBO dbo = IDBOSQL.IDBO.getIDBO(mConn);
dbo.openDatabase();
DataSet ds = dbo.executeSelectSql("select Card_Money,Service_Discount,Good_Discount from Table_CardInfo join Dic_CardType on Table_CardInfo.CardType_ID= Dic_CardType.CardType_ID where Card_NO='"+cardNo+"' and Card_Pwd= '"+cardPwd+"'and card_Status='正常'");
DataTable dt = ds.Tables[0];
string money = dt.Rows[0][0].ToString();
string service_discount = dt.Rows[0][1].ToString();
string good_discount = dt.Rows[0][2].ToString();
dbo.closeDatabase();
return money+","+service_discount+','+good_discount;//此處返回一個多個值拼接成的字符串
}
}
3、在JS中調(diào)用
moneydiscount= AjaxMethod.GetCardMoney(card, pwd).value;
moneydiscount//是一個多個值拼接成的字符串,要獲取多個值的話,可以把該字符串轉(zhuǎn)換為一個數(shù)組,然后去訪問。
arr=moneydiscount.split(","); //這樣的話可以很方便的使用Ajax返回多個值。
4、要把結(jié)果再返回到頁面中
document.getElementById("txtCard_Money1").value=arr[0];
5、以上方法要在Web.config文件中增加
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
今天的內(nèi)容就寫到這里,以后有時間再慢慢寫
1、引用Ajax.dll
2、在App_Code寫具體的方法,最好單獨建立一個類文件,然后寫具體方法。
復制代碼 代碼如下:
public class AjaxMethod www.dbjr.com.cn
{
public AjaxMethod()
{
//
//TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetCardMoney(string cardNo,string cardPwd)
{
string mConn = IConfiguration.getParameter("connectString");
IDBOSQL.IDBO dbo = IDBOSQL.IDBO.getIDBO(mConn);
dbo.openDatabase();
DataSet ds = dbo.executeSelectSql("select Card_Money,Service_Discount,Good_Discount from Table_CardInfo join Dic_CardType on Table_CardInfo.CardType_ID= Dic_CardType.CardType_ID where Card_NO='"+cardNo+"' and Card_Pwd= '"+cardPwd+"'and card_Status='正常'");
DataTable dt = ds.Tables[0];
string money = dt.Rows[0][0].ToString();
string service_discount = dt.Rows[0][1].ToString();
string good_discount = dt.Rows[0][2].ToString();
dbo.closeDatabase();
return money+","+service_discount+','+good_discount;//此處返回一個多個值拼接成的字符串
}
}
3、在JS中調(diào)用
復制代碼 代碼如下:
moneydiscount= AjaxMethod.GetCardMoney(card, pwd).value;
moneydiscount//是一個多個值拼接成的字符串,要獲取多個值的話,可以把該字符串轉(zhuǎn)換為一個數(shù)組,然后去訪問。
arr=moneydiscount.split(","); //這樣的話可以很方便的使用Ajax返回多個值。
4、要把結(jié)果再返回到頁面中
復制代碼 代碼如下:
document.getElementById("txtCard_Money1").value=arr[0];
5、以上方法要在Web.config文件中增加
復制代碼 代碼如下:
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
今天的內(nèi)容就寫到這里,以后有時間再慢慢寫
相關(guān)文章
關(guān)于promise和async用法以及區(qū)別詳解
Promise是一個構(gòu)造函數(shù),我們就可以new Promise()得到一個 Promise的實例,下面這篇文章主要給大家介紹了關(guān)于promise和async用法以及區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-01-01JAVA Web實時消息后臺服務(wù)器推送技術(shù)---GoEasy
本篇文章主要介紹了PHP實現(xiàn)Web實時消息后臺服務(wù)器推送技術(shù),這里整理了詳細的代碼,有需要的小伙伴可以參考下。2016-11-11點擊彈出層效果&彈出窗口后網(wǎng)頁背景變暗效果的實現(xiàn)代碼
本篇文章主要是對點擊彈出層效果&彈出窗口后網(wǎng)頁背景變暗效果的實現(xiàn)代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02