当前位置: 首页 > news >正文

Atlas学习手记(8):调用本地Web Service简单介绍

Atlas对于调用Web Service作了很好的封装,使得我们用JS调用Web Service的工作变得非常的简单,只需要使用WebServiceName.WebMethod()就可以完成调用。本文将通过两个简单的例子来说明这一内容。

主要内容

1.调用简单的Web Service

2.传递复杂类型的数据

 

Atlas对于调用Web Service作了很好的封装,使得我们用JS调用Web Service的工作变得非常的简单,只需要使用WebServiceName.WebMethod()就可以完成调用。本文将通过两个简单的例子来说明这一内容。

一.调用简单的Web Service

这个例子中,我们调用Web Service来返回一个字符串,首先创建一个简单的Web Service,并编写一个接受字符串类型参数的Web Method

None.gif [WebMethod]
None.gif
None.gif
public   string  EchoString( string  s)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return "Hello : " + s;
ExpandedBlockEnd.gif}

创建Web Page,并且添加ScriptManager到页面中,并且在ServiceReference子控件中引入需要的Web Service

None.gif < atlas:ScriptManager  ID ="scriptManager"  runat ="server"  EnableScriptComponents ="true"   >
None.gif
None.gif    
< Services >
None.gif
None.gif        
< atlas:ServiceReference  Path ="SimpleWebService.asmx"   />
None.gif
None.gif    
</ Services >
None.gif
None.gif
</ atlas:ScriptManager >

下面我们就可以在JS中调用Web Service了,注意EchoString方法只有一个参数,这里我们传递了两个,第一个显然是EchoString方法应有的参数,第二个OnComplete则调用方法成功返回时的Callback方法:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/javascript"  language ="JavaScript" > dot.gif
InBlock.gif
InBlock.gif    
function OnbuttonGo_click() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
// Call script proxy passing the input element data
InBlock.gif

InBlock.gif        requestSimpleService 
= SimpleWebService.EchoString(
InBlock.gif
InBlock.gif            document.getElementById('inputName').value,       
//params
InBlock.gif

InBlock.gif            OnComplete    
//Complete event
InBlock.gif

InBlock.gif            );
InBlock.gif
InBlock.gif        
return false;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
function OnComplete(result) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        alert(result);
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif
None.gif
</ script >

编译运行后:

调用:

二.传递复杂类型的数据

上面的例子中,我们只是做了一个最简单的调用Web Service的示例,而实际应用中我们遇到的类型会更加复杂,下面再看一个例子,它将返回一个我们自定义的类型,首先定义一个纯数据类Animal,它不带有操作:

None.gif public   class  Animal
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    String _name;
InBlock.gif
InBlock.gif    String _color;
InBlock.gif
InBlock.gif    
public String Name
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _name; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public String Color
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _color; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _color = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

编写Web Service,接收到该复杂类型后直接返回:

None.gif [WebMethod]
None.gif
None.gif
public  Animal EchoAnimal(Animal a)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return a;
ExpandedBlockEnd.gif}

创建Web Page,并且添加ScriptManager到页面中,并且在ServiceReference子控件中引入需要的Web Service

None.gif < atlas:ScriptManager  runat ="server"  ID ="scriptManager" >
None.gif
None.gif    
< Services >
None.gif
None.gif        
< atlas:ServiceReference  Path ="ComplexWebService.asmx"   />
None.gif
None.gif    
</ Services >
None.gif
None.gif
</ atlas:ScriptManager >

提供给用户输入的界面:

None.gif < h3 >
None.gif
None.gif    Name:
< input  id ="inputName"   />
None.gif
None.gif    Color:
< input  id ="inputColor"   />
None.gif
None.gif    
< input  id ="buttonGo"  type ="button"  value ="GO"  onclick ="return OnbuttonGo_click()"   />
None.gif
None.gif
</ h3 >

现在就可以添加相应的JS了,把返回的结果Alert出来:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/javascript"  language ="JavaScript" > dot.gif
InBlock.gif
InBlock.gif    
function OnbuttonGo_click() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//Call script proxy passing the input element data
InBlock.gif

InBlock.gif        
var i1 = document.getElementById('inputName');
InBlock.gif
InBlock.gif        
var i2 = document.getElementById('inputColor');
InBlock.gif
InBlock.gif        
var object = new Animal();
InBlock.gif
InBlock.gif        object.Name 
= i1.value;
InBlock.gif
InBlock.gif        object.Color 
= i2.value;
InBlock.gif
InBlock.gif
InBlock.gif        requestComplexService 
= ComplexWebService.EchoAnimal(
InBlock.gif
InBlock.gif            object,         
//params
InBlock.gif

InBlock.gif            OnComplete     
//Complete eventt
InBlock.gif

InBlock.gif            );
InBlock.gif
InBlock.gif        
return false;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
function OnComplete(result) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"Name= " + result.Name + " Color= " + result.Color);
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif
None.gif
</ script >

编译运行后:

调用:

可以看到,在Atlas中调用本地Web Service非常的简单,对于调用远程的Web Service又有一些不同,后面会说到,在实际使用中,我们还需要考虑错误、超时等的一些处理[文中的示例来源于Atlas官方网站]。

完整示例下载:http://files.cnblogs.com/Terrylee/WebServiceDemo.rar

转载于:https://www.cnblogs.com/Terrylee/archive/2006/07/30/Atlas_WebService_Introduction.html

相关文章:

  • 诊断排除基本的 TCP/IP 网络问题
  • 数学名师沈赫哲----在台大上课的演讲全文!!
  • 针对 xml 数据类型的 XQuery (摘自SQL Server 2005 联机丛书)
  • C#谜题86: 有害的括号垃圾
  • 思科新logo
  • [转载]asp.net中page对象生命周期和各事件执行顺序
  • SQL Server连接中三个常见的错误分析(转)
  • 微软 DLinq技术来临前的国内 .NET 的 ORM 发展之局势
  • 第二篇 收购与发展
  • 一个小网工的笔记1
  • “应用”按钮,一个让人比较郁闷的设计
  • 越来越冷了
  • 闲谈: 测试报告系统
  • 美国SkillSoft巨资收购NETg公司,巩固全球霸主地位
  • 可爱的扇贝
  • 分享的文章《人生如棋》
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Django 博客开发教程 8 - 博客文章详情页
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • JWT究竟是什么呢?
  • node学习系列之简单文件上传
  • Redis字符串类型内部编码剖析
  • ucore操作系统实验笔记 - 重新理解中断
  • vuex 学习笔记 01
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 扑朔迷离的属性和特性【彻底弄清】
  • 区块链技术特点之去中心化特性
  • 微信开源mars源码分析1—上层samples分析
  • HanLP分词命名实体提取详解
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​插件化DPI在商用WIFI中的价值
  • ​业务双活的数据切换思路设计(下)
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (译) 函数式 JS #1:简介
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • .NET CORE 2.0发布后没有 VIEWS视图页面文件
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .net web项目 调用webService
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET学习教程二——.net基础定义+VS常用设置
  • @Mapper作用
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [20150629]简单的加密连接.txt
  • [8481302]博弈论 斯坦福game theory stanford week 1
  • [BZOJ 1040] 骑士
  • [bzoj 3534][Sdoi2014] 重建
  • [CISCN2019 华东北赛区]Web2
  • [HarekazeCTF2019]encode_and_encode 不会编程的崽
  • [hdu 1711] Number Sequence [kmp]