Flex+Asp.net+HTTPService 交互

1,将Flex嵌入到asp.net的页面中,使用
<embed src="flex_bin/Test.swf" width="650" height="490" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always"></embed>
src为Flex资源的相对位置。
2,Flex使用HTTPService 调用aps.net页面返回的数据
aps.net文件:
protected void Page_Load(object sender, EventArgs e)
{
//System.Web.Caching.Cache chace = new System.Web.Caching.Cache();
if (Request.QueryString["username"] != null)
{
if (Request.QueryString["username"].Equals("xx"))
{
Response.Write("<Result>");
Response.Write("<user>ok</user>");
Response.Write("</Result>");
}
else
{
Response.Write("<Result>");
Response.Write("<user>error</user>");
Response.Write("</Result>");
}
}
}
Flex文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="320" height="219">
<mx:HTTPService showBusyCursor="true"
id="getuser" result="getuserproc();"
url="http://localhost:1866/Default.aspx">
<mx:request>
<username>
{this.txtUserName.text}
</username>
<userpassword>
{this.txtUserPassWord.text}
</userpassword>
</mx:request>
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.AbstractOperation;
public function getuserproc():void
{
var returnValue:String=getuser.lastResult.Result.user;
if(returnValue=="ok")
{
Alert.show("您成功的登录了","提示信息",Alert.OK,this,null,null,Alert.YES);
}
else
{
Alert.show("您的登录失败了","提示信息",Alert.OK,this,null,null,Alert.YES);
}
}
]]>
</mx:Script>
<mx:Panel id="UserRegPanel" x="9.15" y="9.05" width="302"
height="204" layout="absolute">
<mx:Label x="10" y="22" text="用户名:" id="lblUserName"
enabled="true" fontSize="15"/>
<mx:Label x="10" y="64" text="密 码:" id="lblUserPassWord"
enabled="true" fontSize="15"/>
<mx:TextInput x="83" y="22" fontSize="15" id="txtUserName"/>
<mx:TextInput x="83" y="63" fontSize="15" id="txtUserPassWord"/>
<mx:Button x="96.45" y="108" label="登录" width="89" height="36"
fontSize="15" enabled="true" click="getuser.send()">
</mx:Button>
</mx:Panel>
</mx:Application>
注意端口号要与aps.net服务器一致(目前使用自带的调试服务器测试)
3,flex调用aps.net 的Web Service
新建一个web service ,不做任何修改。
flex端的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="320" height="219">
<mx:WebService id="ws" wsdl="http://localhost:1866/WebService1.asmx?wsdl" useProxy="false">
<mx:operation name="HelloWorld" result="getuserproc(ws.getOperation('HelloWorld'));">
</mx:operation>
</mx:WebService>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.AbstractOperation;
public function getuserproc(target:AbstractOperation):void
{
var returnValue:String=target.lastResult.toString();
if(returnValue!=null)
Alert.show("调用成功"+returnValue,"提示信息",Alert.OK,this,null,null,Alert.YES);
else
{
Alert.show("调用失败","提示信息",Alert.OK,this,null,null,Alert.YES);
}
}
]]>
</mx:Script>
<mx:Panel id="UserRegPanel" x="9.15" y="9.05" width="302"
height="204" layout="absolute">
<mx:Label x="10" y="22" text="用户名:" id="lblUserName"
enabled="true" fontSize="15"/>
<mx:Label x="10" y="64" text="密 码:" id="lblUserPassWord"
enabled="true" fontSize="15"/>
<mx:TextInput x="83" y="22" fontSize="15" id="txtUserName"/>
<mx:TextInput x="83" y="63" fontSize="15" id="txtUserPassWord"/>
<mx:Button x="96.45" y="108" label="登录" width="89" height="36"
fontSize="15" enabled="true" click="ws.getOperation('HelloWorld').send()">
</mx:Button>
</mx:Panel>
</mx:Application>
启动aps.net 调试,运行flex。同样注意端口的一致。
Tags: 

延伸阅读

最新评论

发表评论