日期:2014-05-18  浏览次数:20377 次

在客户段使用Javascript调用web服务
按书上写了下面的代码

<WebService(Namespace:= "http://tempuri.org/ ")>   _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>   _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>   _
Public   Class   StocksWebService
        Inherits   System.Web.Services.WebService

        <WebMethod()>   _
        Public   Function   GetStocks(ByVal   CorpNameTxt   As   String)   As   String
                Using   con   As   New   SqlConnection(WebConfigurationManager.ConnectionStrings( "chtNorthwind ").ConnectionString)
                        Dim   SQLCmd   As   SqlCommand   =   con.CreateCommand()

                        SQLCmd.CommandText   =   "SELECT   [持股数目]   FROM   [外资持股明细]   WHERE   [公司名称]   =   @CorpName "

                        SQLCmd.Parameters.Add( "@CorpName ",   Data.SqlDbType.NVarChar,   12).Value   =   CorpNameTxt

                        con.Open()

                        Using   dr   As   SqlDataReader   =   SQLCmd.ExecuteReader(CommandBehavior.SingleResult)
                                If   dr.Read()   Then
                                        Return   dr.GetSqlInt32(0).ToString
                                End   If
                        End   Using
                End   Using

                Return   Nothing
        End   Function
End   Class

可是无法成功,请高人指点那里有错?

------解决方案--------------------
请检查是否少了以下命名空间
Imports System.Web.Script.Services

你的代码再加一个特性
<WebService(Namespace:= "http://tempuri.org/ ")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ScriptService()> _

还有要学会使用失败时的回调函数
------解决方案--------------------
http://blog.csdn.net/fengyecsdn/archive/2007/08/18/1749845.aspx


首先说一下,这个问题和AJAX难以分开.其实现方法和AJAX非常类似.有AJAX经验的朋友就很容易理解.

我这里举例的项目没有直接使用MS的AJAX框架而是只引用了该框架中的一个关键DLL和两段脚本.下边我会清楚的说明JS访问WEB SERVICES的具体流程.

首先:准备一个SERVICE,在SERVICE中要加上下边两个引用.

using System.Web.Script.Services;
using System.Web.Script.Serialization;
这两个引用自System.Web.Extensions.dll
没有的话去MS下载, 在MS的AJAX包中就包含

SERVICE.CS代码中
[ScriptService]
public class ElmService : System.Web.Services.WebService
{
[WebMethod(