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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值