`

.net 下用javascript调用webservice

    博客分类:
  • .NET
阅读更多
.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中,调用其wsdl
addservice.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>
 
 
 
 
 
 
 
 
分享到:
评论

相关推荐

    Javascript调用WebService

    使用javascript 中Ajax技术调用WebService,包括JSP和ASP.NET中两种,其中JSP的WebService使用了XFire框架。 ASP.NET中的WebService做了一个方法示例GetProgress,包含3个参数。 JSP中的WebService做了hello方法的...

    使用javascript调用webservice

    使用JavaScript直接调用Web Service是微软AJAX Extension支持的新特性,在Visual Studio 2005中如果不安装AJAX Extension是不能用的,而AJAX Extension是Visual Studio 2008的标准配置。 由于JavaScript是所有主流...

    从ASP3.0和JavaScript调用WebService

    从ASP3.0和JavaScript调用WebService

    ASP.NET(BS构架) 调用启动 IIS服务端exe程序案例

    二、如果需要启动 客户端 exe程序,则可以考虑使用javascript脚本。 三、案例项目说明 ConsoleAppMain:本案例中需要启动的服务端exe代码 asp.net:本案例中的asp网页代码,启动exe程序的代码在 Default.aspx.cs...

    javascript 调用Web service

    本示例是用微软提供的webservice.htc来完成的,当然用ASP.NET AJAX也能实现,但该示例全部是JAVASCRIPT来做的,简单明了.

    asp.net知识库

    体验.net 2.0 的优雅(1) -- 异步WebService调用 ASP.NET 2.0页面框架的几点新功能 ASP.NET 2.0 中收集的小功能点 asp.net2.0中的webpart使用小记 2.0问题、错误解决办法 ASP.NET 2.0使用Web Part创建应用程序之二...

    VS2008中使用JavaScript调用WebServices

    首先,用VS2008创建一个asp.net网站 其次,项目 右键—&gt;添加新项—&gt;Web 服务 如下图: 就会产生WebService.cs和WebService.asmx两个文件 在WebService.cs中添加代码: 代码如下: using System; using System....

    QCAsp.net通用数据报表源码20140423

    javascript脚本调用webService服务用来显示各式各样的统计报表效果图, 用户也可以录入金额重新进行报表的统计。 里面的统计统计报表效果图 种类繁多(主要包括柱状图、折线图、饼状图、环形图等),展示的效果也很 ...

    Ajax请求WebService跨域问题的解决方案

     用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2、出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容); 3、解决方案: (1) JSONP:只支持GET方式 (2) CROS:...

    JqueryAjaxGenerator:从 .net webservice (.asmx) 生成 jquery ajax 代码

    快速生成javascript代码来调用.Net WebService(.asmx)。 在 Visual Studio 2012 的 javascript 端完全支持自定义对象的智能感知。 易于使用,通过 JSON 传递参数。 在 Javascript 方面有相同的经验,比如在 C# ...

    ASP.NET Night Words

    14.2.5 异步调用webservice 274 14.3 wcf开发 276 14.3.1 定义wcf服务契约和引入 14.3.1 wcf服务契约 277 14.3.2 构架wcf服务,运行wcf 14.3.1 服务 281 14.3.3 生成wcf服务代理的 14.3.1 代码文件 283 ...

    ASP.NET 小技巧(2个)

    1. ASP.NET AJAX 中,如何用 JavaScript 调用服务器端的方法? 这里不是指调用简单的 PageMethod,因为静态方法是不能操作当前页面的控件的,所以静态的 PageMethod 作用就跟普通的 WebService 一样,比较局限。 ...

    2.ASP.NET.2.0.高级编程(第4版) [1/7]

    书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP.NET初级程序员以及准备迁移到ASP.NET 2.0的编程老手。该书与《ASP.NET 2.0入门经典(第4版)》...

    asp.net面试题

    远程逻辑调用,remoing接口只能用在.net中 13.什么是code-behind技术 aspx and cs 14.概述三层结构体系 web/business/dataaccess 15.asp.net如何实现MVC模式,举例说明! web/business/dataaccess ---------...

    ASP.NET2.0高级编程(第4版)1/6

    书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP.NET初级程序员以及准备迁移到ASP.NET 2.0的编程老手。该书与《ASP.NET 2.0入门经典(第4版)》...

    几个 ASP.NET 小技巧

    1. ASP.NET AJAX 中,如何用 JavaScript 调用服务器端的方法? 这里不是指调用简单的 PageMethod,因为静态方法是不能操作当前页面的控件的,所以静态的 PageMethod 作用就跟普通的 WebService 一样,比较局限。 ...

    WebService结合ajax无刷新省市三级联动下拉框

    .NET源码,Ajax相关,Ajax,级联菜单 使用ajax技术结合c#的WebService实现的无刷新盛市、县三级联动下拉框源程序,含控件源代码,工程运行VS2003,具体一点就是使用webservice.htc,然后由JavaScript调用WebService来...

    C#调用C++底层代码

    从功能的角度来看,WCF完全可以看作是ASMX,.Net Remoting,Enterprise Service,WebService,MSMQ等技术的并集。 WPF是Windows Presentation Foundation,微软新发布的Vista操作系统的三大核心开发库之一,其主要...

Global site tag (gtag.js) - Google Analytics