http://singlepine.cnblogs.com/articles/301009.html
使用微软提供的webservice.htc实现通过JavaScript调用WebService.
1.首先从微软网站上下载webservice.htc,我附件源代码也包含,不下载也可以
http://msdn.microsoft.com/workshop/author/webservice/webservice.htc
2.在网页BODY中添加一个DIV,实现对webservice.htc的引用
 <div id="service" style="BEHAVIOR:url(webservice.htc)"></div>
<div id="service" style="BEHAVIOR:url(webservice.htc)"></div>3.编写JavaScript,实现对WebService的引用:
 function window_onload()
function window_onload() 
 
             {
{ service.useService("/Service1.asmx?WSDL","myselect");
                service.useService("/Service1.asmx?WSDL","myselect"); str_province=service.myselect.callService(province_Result,"getProvince");
                str_province=service.myselect.callService(province_Result,"getProvince"); }
            }sElementID.useService(sWebServiceURL, sFriendlyName [, oUseOptions])
useService 参数:
| sElementID | Required. The of the element to which the 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 object. | 
callService语法:
iCallID = sElementID.sFriendlyName.callService( [oCallHandler], fo, oParam)
callService参数:
| sElementID | Required. The of the element to which the behavior is attached. | ||||
| sFriendlyName | Required. The friendly name for the Web Service, which is defined by calling the 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,代码如下
 public class Service1 : System.Web.Services.WebService
public class Service1 : System.Web.Services.WebService
 
     {
{ public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; SqlConnection conn=new SqlConnection(ConnectionString);
        SqlConnection conn=new SqlConnection(ConnectionString);
 public Service1()
        public Service1()
 
         {
{ //CODEGEN: This call is required by the ASP.NET Web Services Designer
            //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent();
            InitializeComponent(); }
        }

 Component Designer generated code#region Component Designer generated code
        Component Designer generated code#region Component Designer generated code 
         //Required by the Web Services Designer
        //Required by the Web Services Designer  private IContainer components = null;
        private IContainer components = null; 
                
 /**//// <summary>
        /**//// <summary> /// Required method for Designer support - do not modify
        /// Required method for Designer support - do not modify /// the contents of this method with the code editor.
        /// the contents of this method with the code editor. /// </summary>
        /// </summary> private void InitializeComponent()
        private void InitializeComponent()
 
         {
{ }
        }

 /**//// <summary>
        /**//// <summary> /// Clean up any resources being used.
        /// Clean up any resources being used. /// </summary>
        /// </summary> protected override void Dispose( bool disposing )
        protected override void Dispose( bool disposing )
 
         {
{ if(disposing && components != null)
            if(disposing && components != null)
 
             {
{ components.Dispose();
                components.Dispose(); }
            } base.Dispose(disposing);
            base.Dispose(disposing);         }
        } 
         #endregion
        #endregion 
        
 getProvince#region getProvince
        getProvince#region getProvince [WebMethod(true)]
        [WebMethod(true)] public string getProvince()
        public string getProvince()
 
         {
{ string sql="select * from province";
            string sql="select * from province"; SqlCommand cmd=new SqlCommand(sql,conn);
            SqlCommand cmd=new SqlCommand(sql,conn); 
 cmd.Connection.Open();
            cmd.Connection.Open(); SqlDataReader dr=cmd.ExecuteReader();
            SqlDataReader dr=cmd.ExecuteReader();
 string s="";
            string s=""; while(dr.Read())
            while(dr.Read())
 
             {
{ s += "," + dr["provinceID"].ToString() + "|" + dr["province"].ToString();
                s += "," + dr["provinceID"].ToString() + "|" + dr["province"].ToString(); }
            } return s;
            return s; }
        }
 #endregion
        #endregion

 getCity#region getCity
        getCity#region getCity [WebMethod(true)]
        [WebMethod(true)] public string getCity(string provinceid)
        public string getCity(string provinceid)
 
         {
{ string str="select * from city where father = '"+provinceid+"'";
            string str="select * from city where father = '"+provinceid+"'"; SqlCommand cmd=new SqlCommand(str,conn);
            SqlCommand cmd=new SqlCommand(str,conn); 
 cmd.Connection.Open();
            cmd.Connection.Open(); SqlDataReader dr=cmd.ExecuteReader();
            SqlDataReader dr=cmd.ExecuteReader();
 string s="";
            string s=""; while(dr.Read())
            while(dr.Read())
 
             {
{ s += "," + dr["cityID"].ToString() + "|" + dr["city"].ToString();
                s += "," + dr["cityID"].ToString() + "|" + dr["city"].ToString(); }
            } return s;
            return s; }
        }
 #endregion
        #endregion

 getArea#region getArea
        getArea#region getArea [WebMethod(true)]
        [WebMethod(true)] public string getArea(string cityid)
        public string getArea(string cityid)
 
         {
{ string str="select * from area where father='"+cityid+"'";
            string str="select * from area where father='"+cityid+"'"; SqlCommand cmd=new SqlCommand(str,conn);
            SqlCommand cmd=new SqlCommand(str,conn); 
 cmd.Connection.Open();
            cmd.Connection.Open(); SqlDataReader dr=cmd.ExecuteReader();
            SqlDataReader dr=cmd.ExecuteReader();
 string s="";
            string s=""; while(dr.Read())
            while(dr.Read())
 
             {
{ s += "," + dr["areaID"].ToString() + "|" + dr["area"].ToString();
                s += "," + dr["areaID"].ToString() + "|" + dr["area"].ToString(); }
            } return s;
            return s; }
        } #endregion
        #endregion }
    } <HTML>
<HTML> <HEAD>
    <HEAD> <title>jsWebServices</title>
        <title>jsWebServices</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 <script language="javascript">
        <script language="javascript">
 <!--
        <!--             var str_province;
            var str_province; var str_city;
            var str_city; var str_area;
            var str_area;
 function window_onload()
            function window_onload() 
 
             {
{ service.useService("/Service1.asmx?WSDL","myselect");
                service.useService("/Service1.asmx?WSDL","myselect"); str_province=service.myselect.callService(province_Result,"getProvince");
                str_province=service.myselect.callService(province_Result,"getProvince"); }
            }
 function province_Result(result)
            function province_Result(result)
 
             {
{                 if(!result.error)
                if(!result.error)
 
               {
{                   document.all("select_province").length=0;
                document.all("select_province").length=0;                 if(result.value.substring(0,1)==",")
                if(result.value.substring(0,1)==",") result.value =result.value.substring(1,result.length);
                        result.value =result.value.substring(1,result.length); var piArray = result.value.split(',');
                var piArray = result.value.split(','); for(var i=0;i<piArray.length;i++)
                for(var i=0;i<piArray.length;i++)
 
                 {
{ var ary1 = piArray[i].toString().split('|');
                  var ary1 = piArray[i].toString().split('|'); document.all("select_province").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                  document.all("select_province").options.add(new Option(ary1[1].toString(),ary1[0].toString())); }
                }                 }
              } }
            }
 function province_onchange()
            function province_onchange() 
 
             {
{ var province=document.getElementById("select_province");
                var province=document.getElementById("select_province"); var pindex = province.selectedIndex;
                var pindex = province.selectedIndex; var pValue = province.options[pindex].value;
                var pValue = province.options[pindex].value; var pText  = province.options[pindex].text;
                var pText  = province.options[pindex].text; str_city=service.myselect.callService(city_Result,"getCity",pValue);
                str_city=service.myselect.callService(city_Result,"getCity",pValue); }
            }
 function city_onchange()
            function city_onchange() 
 
             {
{ var city=document.getElementById("select_city");
                var city=document.getElementById("select_city"); var cindex = city.selectedIndex;
                var cindex = city.selectedIndex; var cValue = city.options[cindex].value;
                var cValue = city.options[cindex].value; var cText  = city.options[cindex].text;
                var cText  = city.options[cindex].text; str_area=service.myselect.callService(area_Result,"getArea",cValue);
                str_area=service.myselect.callService(area_Result,"getArea",cValue); }
            }
 function city_Result(result)
            function city_Result(result)
 
             {
{ if(!result.error)
                if(!result.error)
 
               {
{ document.all("select_city").length=0;
                document.all("select_city").length=0; if(result.value.substring(0,1)==",")
                if(result.value.substring(0,1)==",") result.value =result.value.substring(1,result.length);
                        result.value =result.value.substring(1,result.length); var piArray = result.value.split(",");
                var piArray = result.value.split(","); for(var i=0;i<piArray.length;i++)
                for(var i=0;i<piArray.length;i++)
 
                 {
{ var ary1 = piArray[i].toString().split("|");
                  var ary1 = piArray[i].toString().split("|"); document.all("select_city").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                  document.all("select_city").options.add(new Option(ary1[1].toString(),ary1[0].toString())); }
                } }
              } }
            }
 function area_Result(result)
            function area_Result(result)
 
             {
{ if(!result.error)
                if(!result.error)
 
               {
{ document.all("select_area").length=0;
                document.all("select_area").length=0; if(result.value.substring(0,1)==",")
                if(result.value.substring(0,1)==",") result.value =result.value.substring(1,result.length);
                        result.value =result.value.substring(1,result.length); var piArray = result.value.split(",");
                var piArray = result.value.split(","); for(var i=0;i<piArray.length;i++)
                for(var i=0;i<piArray.length;i++)
 
                 {
{ var ary1 = piArray[i].toString().split("|");
                  var ary1 = piArray[i].toString().split("|"); document.all("select_area").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                  document.all("select_area").options.add(new Option(ary1[1].toString(),ary1[0].toString())); }
                } }
              } }
            } //-->
            //--> </script>
        </script> </HEAD>
    </HEAD> <body MS_POSITIONING="GridLayout" onload="return window_onload()">
    <body MS_POSITIONING="GridLayout" onload="return window_onload()"> <div id="service" style="BEHAVIOR:url(webservice.htc)"></div>
        <div id="service" style="BEHAVIOR:url(webservice.htc)"></div> <form id="Form1" method="post" runat="server">
        <form id="Form1" method="post" runat="server"> <SELECT id="select_province" onchange="province_onchange();" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 128px; POSITION: absolute; TOP: 16px">
            <SELECT id="select_province" onchange="province_onchange();" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 128px; POSITION: absolute; TOP: 16px"> <OPTION selected></OPTION>
                <OPTION selected></OPTION> </SELECT><SELECT id="select_city" onchange="city_onchange();" style="Z-INDEX: 102; LEFT: 160px; WIDTH: 128px; POSITION: absolute; TOP: 16px">
            </SELECT><SELECT id="select_city" onchange="city_onchange();" style="Z-INDEX: 102; LEFT: 160px; WIDTH: 128px; POSITION: absolute; TOP: 16px"> <OPTION selected></OPTION>
                <OPTION selected></OPTION> </SELECT><SELECT id="select_area" style="Z-INDEX: 103; LEFT: 304px; WIDTH: 128px; POSITION: absolute; TOP: 16px">
            </SELECT><SELECT id="select_area" style="Z-INDEX: 103; LEFT: 304px; WIDTH: 128px; POSITION: absolute; TOP: 16px"> <OPTION selected></OPTION>
                <OPTION selected></OPTION> </SELECT>
            </SELECT> </form>
        </form> </body>
    </body> </HTML>
</HTML>7.数据库脚本
 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[area]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[area]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[area]
drop table [dbo].[area] GO
GO
 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[province]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[province]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[province]
drop table [dbo].[province] GO
GO
 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[city]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[city]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[city]
drop table [dbo].[city] GO
GO
 CREATE TABLE [dbo].[area] (
CREATE TABLE [dbo].[area] ( [id] [int] NOT NULL ,
    [id] [int] NOT NULL , [areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [area] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
    [area] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL , [father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
    [father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL  ) ON [PRIMARY]
) ON [PRIMARY] GO
GO
 CREATE TABLE [dbo].[province] (
CREATE TABLE [dbo].[province] ( [id] [int] NOT NULL ,
    [id] [int] NOT NULL , [provinceID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
    [provinceID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL , [province] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL
    [province] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL  ) ON [PRIMARY]
) ON [PRIMARY] GO
GO
 CREATE TABLE [dbo].[city] (
CREATE TABLE [dbo].[city] ( [id] [int] NOT NULL ,
    [id] [int] NOT NULL , [cityID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
    [cityID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL , [city] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [city] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
    [father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL  ) ON [PRIMARY]
) ON [PRIMARY] GO
GO9.源代码下载/Files/singlepine/jsWebServices.rar