当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  .net 下用javascript调用webservice

 .net 下用javascript调用webservice

点击次数:30 次 发布日期:2008-11-26 12:50:27 作者:源代码网
源代码网推荐      .net 下用javascript调用webservice的话,要用到webservice behavior。下面以一个例子讲解之,比较简单
源代码网推荐  
源代码网推荐  1 、首先,要创建一个webservice,比如
源代码网推荐  
源代码网推荐  <%@ WebService Language="C#" class=MyMath %>
源代码网推荐  using System;
源代码网推荐  using System.Web.Services;
源代码网推荐  public class MyMath {
源代码网推荐  [WebMethod]
源代码网推荐  public int add(int a, int b)
源代码网推荐  {
源代码网推荐  return a + b;
源代码网推荐  }
源代码网推荐  [WebMethod]
源代码网推荐  public int subtract(int a, int b)
源代码网推荐  {
源代码网推荐  return a - b;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  然后发布,先得到其wsdl。
源代码网推荐  2、首先,我们要下载webbehavior.htc这个文件(可以到http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp.)
源代码网推荐  去下载,然后放到你的web当前目录下
源代码网推荐  然后在要调用webserice的页面中,修改如下
源代码网推荐  <body>
源代码网推荐  <div id="addservice" style="behavior:url(webservice.htc)"></div>
源代码网推荐  </body>
源代码网推荐  这里我们将div id命名为有意义的名称,并且指定style为 webservice行为。接着,我们要书写javascript来调用webserice了:
源代码网推荐  首先,我们在javascript中,调用其wsdladdservice.useService("http://localhost/services/math.asmx?WSDL","MyMath");
源代码网推荐  使用id.useService(WSDLL路径,简单的命名方式);
源代码网推荐  我们之前设定的id是addservice,而为了给客户端调用方便,我们这里起了名称,叫MyMath。而为了保证能正确调用webserice,必须在body里的onload事件里,马上加载处理webservice调用的javascript,如下
源代码网推荐  <script language="JavaScript">
源代码网推荐  function init()
源代码网推荐  {
源代码网推荐  addservice.useService("http://localhost/services/math.asmx?WSDL","MyMath"); }
源代码网推荐  </script>
源代码网推荐  <body onload="init()">
源代码网推荐  <div id="service" style="behavior:url(webservice.htc)">
源代码网推荐  </div>
源代码网推荐  </body>
源代码网推荐  
源代码网推荐   在上面,我们通过webservice行为,首先得到了返回webservice的wsdl,接下来我们要进行调用了,调用的格式如下: iCallID = id.FriendlyName.callService([CallbackHandler,] "MethodName", Param1, Param2, ...);
源代码网推荐  这里id是我们在div里设置的id,而FridndbyName是我们刚才为方面而起的命,这里就是MyMath了,而CallbackHandler是使用回调函数的过程名,如果无设置的话,则默认是使用onresult所调用的方法来进行处理,下文会讲到,而param1,,param2等则是说传入的参数了,如:
源代码网推荐  <SCRIPT language="JavaScript">
源代码网推荐  // All these variables must be global,
源代码网推荐  // because they are used in both init() and onresult().
源代码网推荐  var iCallID = 0;
源代码网推荐  var intA = 5;
源代码网推荐  var intB = 6;
源代码网推荐  function init()
源代码网推荐  {
源代码网推荐  // Establish the friendly name "MyMath" for the WebServiceURL
源代码网推荐  service.useService("/services/math.asmx?WSDL","MyMath");
源代码网推荐  // The following method doesn"t specify a callback handler, so onWSresult() is used
源代码网推荐  iCallID = service.MyMath.callService("add", intA, intB);
源代码网推荐  }
源代码网推荐  function onWSresult()
源代码网推荐  {
源代码网推荐  // if there is an error, and the call came from the call() in init()
源代码网推荐  if((event.result.error)&&(iCallID==event.result.id))
源代码网推荐  {
源代码网推荐  // Pull the error information from the event.result.errorDetail properties
源代码网推荐  var xfaultcode = event.result.errorDetail.code;
源代码网推荐  var xfaultstring = event.result.errorDetail.string;
源代码网推荐  var xfaultsoap = event.result.errorDetail.raw;
源代码网推荐  // Add code to handle specific error codes here
源代码网推荐  }
源代码网推荐  // if there was no error, and the call came from the call() in init()
源代码网推荐  else if((!event.result.error) && (iCallID == event.result.id))
源代码网推荐  {
源代码网推荐  // Show the arithmetic!
源代码网推荐  alert(intA + " + " + intB + " = " + event.result.value);
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  alert("Something else fired the event!");
源代码网推荐  }
源代码网推荐  }
源代码网推荐  </SCRIPT>
源代码网推荐  <body onload="init()">
源代码网推荐  <div id="service" style="behavior:url(webservice.htc)" onresult="onWSresult()">
源代码网推荐  </div>
源代码网推荐  </body>
源代码网推荐  
源代码网推荐  注意,用onresult方式返回的话,要在div部分的onresult中指定处理的方法,这里是用onWsresult()方法,其中根据返回的信息来判断是否出错,出错的话则显示。
源代码网推荐   如果用回调的话,则如下处理
源代码网推荐  <SCRIPT language="JavaScript">
源代码网推荐  // All these variables must be global,
源代码网推荐  // because they are used in both init() and onResult().
源代码网推荐  var iCallID = 0;
源代码网推荐  var intA = 5;
源代码网推荐  var intB = 6;
源代码网推荐  function init()
源代码网推荐  {
源代码网推荐  // Establish the friendly name "MyMath" for the WebServiceURL
源代码网推荐  service.useService("/services/math.asmx?WSDL","MyMath");
源代码网推荐  // The following uses a callback handler named "mathResults"
源代码网推荐  iCallID = service.MyMath.callService(mathResults, "add", intA, intB);
源代码网推荐  }
源代码网推荐  function mathResults(result)
源代码网推荐  {
源代码网推荐  // if there is an error, and the call came from the call() in init()
源代码网推荐  if(result.error)
源代码网推荐  {
源代码网推荐  // Pull the error information from the event.result.errorDetail properties
源代码网推荐  var xfaultcode = result.errorDetail.code;
源代码网推荐  var xfaultstring = result.errorDetail.string;
源代码网推荐  var xfaultsoap = result.errorDetail.raw;
源代码网推荐  // Add code to handle specific error codes here
源代码网推荐  }
源代码网推荐  // if there was no error
源代码网推荐  else
源代码网推荐  {
源代码网推荐  // Show the arithmetic
源代码网推荐  alert(intA + " + " + intB + " = " + result.value);
源代码网推荐  }
源代码网推荐  }
源代码网推荐  </SCRIPT>
源代码网推荐  <body onload="init()">
源代码网推荐  <div id="service" style="behavior:url(webservice.htc)">
源代码网推荐  </div>
源代码网推荐  </body>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华