点击这里给我发消息 点击这里给我发消息

AJAX在客户端Javascript中调用Webservice

添加时间:2010-1-5
    相关阅读: 设计 WEB 页面 HTML JavaScript C#

 版本为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("he", "sei", livebaby@163.com);
dt.Rows.Add("mei", "wu", blog@163.com);
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>
/// http://meil.livebaby.cn
/// </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>
6 </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>

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册