加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex中调用webservice的两种方法

发布时间:2020-12-15 04:09:44 所属栏目:百科 来源:网络整理
导读:1,用C#写好一个webservice,包含两个方法: [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public int GetSum(int a,int b) { int c = 0; c = a + b; return c; } 2,通过mxml配置,调用 fx:Declarations!-- 将非可视元素

1,用C#写好一个webservice,包含两个方法:

    [WebMethod]
    public string HelloWorld()
   {
        return "Hello World";
    }

    [WebMethod]
    public int GetSum(int a,int b)
    {
        int c = 0;
        c = a + b;
        return c;
    }

2,通过mxml配置,调用

	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<s:WebService id="myWebService" wsdl="http://localhost:4358/WebServicetest/WebService.asmx?wsdl">
				<s:operation name="GetSum" resultFormat="object" result="myresultTwo(event)"/>
				<s:operation name="HelloWorld" resultFormat="object" result="myresultTwo(event)"/>
		</s:WebService>
	</fx:Declarations>
	<s:Button id="myOne" x="184" y="177" width="131" height="47" label="MethodOne"
			  click="myOne_clickHandler(event)"/>
	<s:Button id="myTwo" x="393" y="177" width="131" height="47" label="MethodTwo"
			  click="myTwo_clickHandler(event)"/>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.soap.WebService;
			
			public function myresultOne(event:ResultEvent):void
			{
				Alert.show(event.result.toString());
			}
			public function myresultTwo(event:ResultEvent):void
			{
				Alert.show(event.result.toString());
			}

			protected function myOne_clickHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				myWebService.HelloWorld();
			}
			
			protected function myTwo_clickHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				myWebService.GetSum(7,9);
				
			}
			
		]]>
	</fx:Script>

3,通过AS调用

			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				var myser:WebService=new WebService();
				myser.wsdl="http://localhost:4358/WebServicetest/WebService.asmx?wsdl";
				myser.loadWSDL();
				myser.getOperation("HelloWorld").addEventListener(ResultEvent.RESULT,myresultOne);
				myser.getOperation("HelloWorld").send();
				myser.getOperation("GetSum").addEventListener(ResultEvent.RESULT,myresultTwo);
				myser.getOperation("GetSum").send(2,3);	
			}
			
			public function myresultOne(event:ResultEvent):void
			{
				Alert.show(event.result.toString());
			}
			public function myresultTwo(event:ResultEvent):void
			{
				Alert.show(event.result.toString());
			}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读