下面做一个简单的demo,a+b=c这样的形式,把a 和b的数据发送到web service上,然后再回传到flex,显示出来。
首先是flex的代码如下:(可以用项目来做,也可以单个文件通过mxmlc来编译)
<?
xml version="1.0" encoding="utf-8"
?>
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="absolute" >
< mx:WebService wsdl ="http://localhost/flexService/Service1.asmx?wsdl" showBusyCursor ="true" id ="ws" >
< mx:operation name ="HelloWorld" >
< mx:request xmlns ="" >
< a > {a.text} </ a >
< b > {b.text} </ b >
</ mx:request >
</ mx:operation >
</ mx:WebService >
< mx:TextInput x ="72" y ="110" id ="a" />
< mx:TextInput x ="72" y ="170" id ="b" />
< mx:TextInput x ="72" y ="239" id ="c" text ="{ws.HelloWorld.lastResult}" />
< mx:Button x ="72" y ="200" label ="=" fontSize ="12" click ="ws.HelloWorld.send()" />
< mx:Label x ="72" y ="140" text ="+" fontSize ="12" width ="25" height ="22" />
</ mx:Application >
再是net上的代码:(新建web service项目,把helloWorld改过来,偷懒-_-!)
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="absolute" >
< mx:WebService wsdl ="http://localhost/flexService/Service1.asmx?wsdl" showBusyCursor ="true" id ="ws" >
< mx:operation name ="HelloWorld" >
< mx:request xmlns ="" >
< a > {a.text} </ a >
< b > {b.text} </ b >
</ mx:request >
</ mx:operation >
</ mx:WebService >
< mx:TextInput x ="72" y ="110" id ="a" />
< mx:TextInput x ="72" y ="170" id ="b" />
< mx:TextInput x ="72" y ="239" id ="c" text ="{ws.HelloWorld.lastResult}" />
< mx:Button x ="72" y ="200" label ="=" fontSize ="12" click ="ws.HelloWorld.send()" />
< mx:Label x ="72" y ="140" text ="+" fontSize ="12" width ="25" height ="22" />
</ mx:Application >
[WebMethod]
public int HelloWorld( int a, int b)
{
return a + b;
}
因为在flex里运用了绑定(binding)技术,不需要像在flash里考虑他是否已经把数据load下来,它在得到数据后会自动更新到组件上,减少了程序员的工作量。
public int HelloWorld( int a, int b)
{
return a + b;
}
再一点是,web service可以返回一个类,它可以和flex里的类相匹配,很方便整体数据的传递。
接受数据的格式有多种,可以看我以前的文章, Flex中获取web service返回值的多种方法