ASP.NET 2.0 AJAX中能夠在客戶端js中很方便地呼叫伺服器Webservice,以下為一些呼叫的範例。作者安裝的ASP.NET 2.0 AJAX
版本為AJAX November CTP。
三個範例分別為:
1 帶參數的WS方法
2 不帶參數的WS方法
3 參數類型為DataTable的WS方法
一、WebMethod
注意要點:
1 WebMethod類別需要新增命名空間Microsoft.Web.Script.Services,此空間需要參考Microsoft.Web.Preview.dll
2 類別聲明加入標籤[ScriptService]
3 在Asp.net 2.0裡可以直接用DataTable當回傳類型了,但是需要在Web.config檔加入序列化轉換器的屬性。 DataSet、DataTable、DataRow均有轉換器
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
WEB服務1:WS1
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Script.Services;
using System.Data;
/**//// <summary>
/// WS1 的摘要說明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WS1 : System.Web.Services.WebService {
public WS1 () {
//如果使用設計的元件,請取消註解下列行
//InitializeComponent();
}
[WebMethod]
public string ServerTime()
{
return String.Format("now: {0}", DateTime.Now);
}
[WebMethod]
public DataTable GetDataTable()
{
DataTable dt = new DataTable("Person");
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("LastName", typeof(string)));
dt.Columns.Add(new DataColumn("Email", typeof(string)));
dt.Rows.Add("kui", "he", " [email protected] ");
dt.Rows.Add("ren", "chao", " [email protected] ");
return dt;
}
}
WEB服務2:WS
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Script.Services;
/**//// <summary>
/// WS 的摘要說明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WS : System.Web.Services.WebService {
public WS () {
//如果使用設計的元件,請取消註解下列行
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(String query)
{
string inputString = Server.HtmlEncode(query);
if (!String.IsNullOrEmpty(inputString))
{
return String.Format("hello, {0}. ", inputString);
}
else
{
return "query string is null or empty";
}
}
}
二、前台頁面:
注意要點:
需要使用的後台WebService的方法都設定在下列位置
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WS.asmx" />
<asp:ServiceReference Path="~/WS1.asmx" />
</Services>
</asp:ScriptManager>
Default頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1// EN" " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript" src="js.js">
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WS.asmx" />
<asp:ServiceReference Path="~/WS1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="dd();return false;" />
<div id="time">
</div>
<div id="List1">
<asp:DropDownList ID="ddl1" runat="server" Width="187px">
</asp:DropDownList>
</div>
</div>
</form>
</body>
</html>
三、JavaScript程式:
注意要點:
AJAX November CTP 需要用eval() 方法轉換成一個DataTable物件(並且要裁掉最前面的"("),而AJAX December CTP 支援以下方法轉換「Sys.Preview.Data.DataTable.parseFromJson(result) ”
function dd()
{
WS.HelloWorld(
'hekui',
function(result)
{
alert(result);
}
);
WS1.ServerTime(
function(result)
{
alert(result);
var divTime = document.getElementById("time");
divTime.innerHTML = result;
}
);
WS1.GetDataTable(
function(result)
{
// 取得到下拉框控件
var List = document.getElementById("ddl1");
//AJAX November CTP 需要用eval() 方法轉換成一個DataTable物件(並且要裁掉最前面的"(")
var Text= result.dataArray.substring(0,result.dataArray.length -1);
var Table = eval( Text);
//AJAX December CTP 支援以下方法轉換
// var Table = Sys.Preview.Data.DataTable.parseFromJson(result);
//清除下拉框原有清單項
for (x=List.options.length-1; x > -1; x--)
{
List.remove(0);
}
//從取得的DataTable新增資料到下拉方塊清單項
for (x=0; x < Table.length; x++ )
{
//取得每一行
var Row = Table[x];
//建立一個清單項
var option = document.createElement("option");
//清單項目顯示文字賦值
option.text = Row.Name + " " + Row.LastName;
//清單項目選項值賦值
option.value = Row.Email;
//判斷瀏覽器類型,進行項目添加
if ( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
List.add(option);
else
List.add(option, null);
}
}
);
}
範例程式碼: http://www.cnblogs.com/heekui/archive/2007/01/10/616332.html