惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
GbyAI
GbyAI
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
B
Blog
博客园 - 叶小钗
V
Visual Studio Blog
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
博客园 - Franky
V
V2EX
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
IT之家
IT之家
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
N
Netflix TechBlog - Medium
博客园 - 【当耐特】

博客园 - .............

验证码生成器 十年编程无师自通 模仿桌面外观 Web服务是否失去前进目标 “开源”SOA正在改写IT规划方程式 C#实现的18位身份证格式验证算法 - ............. - 博客园 怎样使用Junit Framework进行单元测试的编写 从一个项目谈XP在国内的应用 - ............. - 博客园 AOP是什么? 在C#中使用Conditional元数据attribute来实现Debug代码 - ............. - 博客园 IBM CRM 系统解决方案 HR软件之我见-小议目前HR供应商 Atlas学习手记(11):使用ModalPopup Extender Atlas学习手记(10):使用AlwaysVisibleControl Extender Atlas学习手记(9):异步调用Page Method Atlas学习手记(7):使用DragOverlayExtender实现拖放功能 Atlas学习手记(6):使用Atlas UpdateProgress控件 Atlas学习手记(5):使用服务端定时控件TimerControl Atlas学习手记(4):使用AutoComplete Extender实现自动完成功能 Atlas学习手记(3):由UpdatePanel开始
Atlas学习手记(8):调用本地Web Service简单介绍
............. · 2006-08-03 · via 博客园 - .............

转自 TerryLee技术空间
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

[WebMethod]

public string EchoString(string s)

{
    
return "Hello : " + s;
}

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

<atlas:ScriptManager ID="scriptManager" runat="server" EnableScriptComponents="true" >

    
<Services>

        
<atlas:ServiceReference Path="SimpleWebService.asmx" />

    
</Services>

</atlas:ScriptManager>

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

<script type="text/javascript" language="JavaScript">

    
function OnbuttonGo_click() 

    
{

        
// Call script proxy passing the input element data

        requestSimpleService 
= SimpleWebService.EchoString(

            document.getElementById('inputName').value,       
//params

            OnComplete    
//Complete event

            );

        
return false;

    }


 

    
function OnComplete(result) 

    
{

        alert(result);

    }


</script>

编译运行后:

调用:

二.传递复杂类型的数据

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

public class Animal

{
    String _name;

    String _color;

    
public String Name

    
{
        
get return _name; }

        
set { _name = value; }
    }


    
public String Color

    
{
        
get return _color; }

        
set { _color = value; }
    }

}

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

[WebMethod]

public Animal EchoAnimal(Animal a)

{
    
return a;
}

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

<atlas:ScriptManager runat="server" ID="scriptManager">

    
<Services>

        
<atlas:ServiceReference Path="ComplexWebService.asmx" />

    
</Services>

</atlas:ScriptManager>

提供给用户输入的界面:

<h3>

    Name:
<input id="inputName" />

    Color:
<input id="inputColor" />

    
<input id="buttonGo" type="button" value="GO" onclick="return OnbuttonGo_click()" />

</h3>

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

<script type="text/javascript" language="JavaScript">

    
function OnbuttonGo_click() 

    
{
        
//Call script proxy passing the input element data

        
var i1 = document.getElementById('inputName');

        
var i2 = document.getElementById('inputColor');

        
var object = new Animal();

        object.Name 
= i1.value;

        object.Color 
= i2.value;


        requestComplexService 
= ComplexWebService.EchoAnimal(

            object,         
//params

            OnComplete     
//Complete eventt

            );

        
return false;

    }


    
function OnComplete(result) 

    
{
        alert(
"Name= " + result.Name + " Color= " + result.Color);

    }


</script>

编译运行后:

调用:

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

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