
使用微软提供的webservice.htc实现通过JavaScript调用WebService.
1.首先从微软网站上下载webservice.htc,我附件源代码也包含,不下载也可以
http://msdn.microsoft.com/workshop/author/webservice/webservice.htc
2.在网页BODY中添加一个DIV,实现对webservice.htc的引用

3.编写JavaScript,实现对WebService的引用:







sElementID.useService(sWebServiceURL, sFriendlyName [, oUseOptions])
useService 参数:
sElementID | Required. The id of the element to which the WebService behavior is attached. | ||||||||
sWebServiceURL | Required. String specifying the URL of the Web Service, using one of the following path types. See the examples section, where several variations of this parameter are shown.
| ||||||||
sFriendlyName | Required. String representing a friendly name for the Web Service URL. | ||||||||
oUseOptions | Optional. An instance of the useOptions object. |
callService语法:
iCallID = sElementID.sFriendlyName.callService( [oCallHandler], fo, oParam)
callService参数:
sElementID | Required. The id of the element to which the WebService behavior is attached. | ||||
sFriendlyName | Required. The friendly name for the Web Service, which is defined by calling the useService method. | ||||
oCallHandler | Optional. Name of a script callback function that handles the results returned from this instance of the method call. | ||||
fo | Required. One of the following possible values.
| ||||
oParam | Required. One or more comma-delimited parameters, which are passed to the method name specified by fo. |
4.建立WebService,代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
namespace WebApplication1
{
/// <summary>
/// Summary description for Service1.
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public static string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath("area.mdb");
OleDbConnection conn = new OleDbConnection(ConnectionString);
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region getProvince
[WebMethod(true)]
public string getProvince()
{
string sql="select * from province";
OleDbCommand cmd=new OleDbCommand(sql,conn);
cmd.Connection.Open();
OleDbDataReader dr=cmd.ExecuteReader();
string s="";
while(dr.Read())
{
s += "," + dr["provinceID"].ToString() + "|" + dr["province"].ToString();
}
return s;
}
#endregion
#region getCity
[WebMethod(true)]
public string getCity(string provinceid)
{
string str="select * from city where father = '"+provinceid+"'";
OleDbCommand cmd=new OleDbCommand(str,conn);
cmd.Connection.Open();
OleDbDataReader dr=cmd.ExecuteReader();
string s="";
while(dr.Read())
{
s += "," + dr["cityID"].ToString() + "|" + dr["city"].ToString();
}
return s;
}
#endregion
#region getArea
[WebMethod(true)]
public string getArea(string cityid)
{
string str="select * from area where father='"+cityid+"'";
OleDbCommand cmd=new OleDbCommand(str,conn);
cmd.Connection.Open();
OleDbDataReader dr=cmd.ExecuteReader();
string s="";
while(dr.Read())
{
s += "," + dr["areaID"].ToString() + "|" + dr["area"].ToString();
}
return s;
}
#endregion
}
}
5.建立测试页面






























































































































6.引用webservicers
7.数据库脚本
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[area]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[area]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[province]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[province]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[city]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[city]
GO
CREATE TABLE [dbo].[area] (
[id] [int] NOT NULL ,
[areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[area] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[province] (
[id] [int] NOT NULL ,
[provinceID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[province] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[city] (
[id] [int] NOT NULL ,
[cityID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[city] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
8.下载真实数据/Files/singlepine/area.rar
9.源代码下载/Files/singlepine/jsWebServices.rar