HttpService 传递参数的方法

来源:互联网 发布:淘宝上架商品没有品牌 编辑:程序博客网 时间:2024/05/01 05:45

1  使用 URLVariables:

 

   例如:

  flex 端

   var  httpService:HTTPService=new HTTPService();
   var params:URLVariables=new URLVariables();
   params.test_id=sTestId;
   params.password=sPassword;
   httpService.url=sServerHostUrl_ + TEST_STUDENT_URL;
   httpService.method=HTTP_SENT_POST;
   httpService.resultFormat=HTTPService.RESULT_FORMAT_E4X;
   httpService.addEventListener(HTTP_RESULT_METHOD, getHttpResultByIDAndPass);
   httpService.addEventListener(HTTP_RESULT_FAULT, httpFault);
   httpService.send(params);

 

服务端:

   接受参数:  test_id  password.

 

2 使用flex 本身.

用actionscript给服务器请求添加参数难免会很麻烦,使用mx:request标签就可以解决这一问题,可以把他
嵌套到HTTPService标签中实现参数的提交。如下例所示:

 flex:

     <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    horizontalAlign="center"
    verticalAlign="middle">
 <mx:Script>
  <![CDATA[
   import mx.rpc.events.ResultEvent;
   import mx.controls.Alert;

   private function goLogin():void
   {
    login.send();
   }

   private function resultHandler(event:ResultEvent):void
   {
    Alert.show(event.result.toString());
   }
  ]]>
 </mx:Script>
 <mx:HTTPService id="login"
     method="POST"
     showBusyCursor="true"
     url="http://localhost/php/login.php"
     result="resultHandler(event)">
 

  <mx:request xmlns="">
   <mx:username>  
                        {username.text}  
                   </mx:username>
   <mx:userpwd>  
                      {userpwd.text}  
                 </mx:userpwd>
  </mx:request>

 

或者:

 

   <mx:request >
   <username>  
                        {username.text}  
                   </username>
   <userpwd>  
                      {userpwd.text}  
                 </userpwd>
  </mx:request>

 

<!-- username  和   userpwd  就是服务器端  需要的参数名称-->
 </mx:HTTPService>
 <mx:Panel width="310"
     height="265"
     layout="absolute"
     title="登录"
     fontSize="12"
     fontWeight="normal"
     x="296"
     y="153">
  <mx:TextInput x="93"
       y="51"
       id="username"
       fontSize="12"/>
  <mx:TextInput x="92"
       y="95"
       id="userpwd"
       fontSize="12"
       displayAsPassword="true"/>
  <mx:Button x="73"
       y="154"
       label="登录"
       id="btn1"
       click="goLogin()"
       fontWeight="normal"
       fontSize="12"/>
  <mx:Label x="32"
      y="53"
      text="用户名:"
      fontSize="12"/>
  <mx:Label x="43"
      y="97"
      text="密码:"
      fontSize="12"/>
  <mx:Button x="154"
       y="154"
       label="注册"
       fontSize="12"
       fontWeight="normal"
       id="btn2"/>
  <mx:Label x="10"
      y="10"
      text="测试用 用户名 User 密码 123456"
      fontSize="12"
      width="243"/>
 </mx:Panel>
</mx:Application>

php 服务端:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>flex login</title>  
</head>  
 
<body>  
<?php       
$return="";  
if(isset($_POST[username]) && isset($_POST[userpwd])){  
 if ("User"==$_POST[username] && "123456"==$_POST[userpwd])  
    $return="ok";  
 else 
    $return="error";  
}  
$xml_return = '<users>';  
$xml_return.= '<a>'.$return.'</a>';  
$xml_return.= '</users>';  
echo $xml_return;   

 
?>  
</body>  
</html>