Flex的httpServices詳解

As3代碼 複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">   
  3. <mx:Script>   
  4.    <![CDATA[   
  5.     import mx.rpc.events.FaultEvent;   
  6.     import mx.rpc.events.ResultEvent;   
  7.        
  8.     private function onReSet(evt:Event):void{   
  9.      username.text="";   
  10.      password.text="";   
  11.      username.setFocus();   
  12.     }   
  13.     public function onSubMit(evt:Event):void{   
  14.      if(username.text.length<4||username.text.length>12){   
  15.       username.setFocus();   
  16.       return;   
  17.      }   
  18.      if(password.text.length<4||password.text.length>12){   
  19.       password.setFocus();   
  20.       return;   
  21.      }   
  22.      //Alert.show("usernmae="+username.text,"提示");   
  23.      //連接數據庫服得用戶信息   
  24.      login.send();   
  25.         
  26.     }   
  27.     private function login_success(evt:ResultEvent):void{   
  28.      conShow.text+=login.lastResult.toString();   
  29.     }   
  30.     private function login_fault(evt:FaultEvent):void{   
  31.        conShow.text+=evt.fault.toString();   
  32.     }   
  33.       
  34.    ]]>   
  35. </mx:Script>   
  36. <mx:Style>   
  37.    .errorTip{   
  38.     fontSize:12px;   
  39.    }   
  40.      
  41. </mx:Style>   
  42. <mx:StringValidator id="reqValid" required="true" source="{username}" property="text" minLength="4" maxLength="12" tooLongError="輸入大長了,不要超過12個字"  
  43. tooShortError="輸入太短了,不能少於4個字" />   
  44. <mx:StringValidator id="reqValid2" required="true" source="{password}" property="text" minLength="4" maxLength="12" tooLongError="輸入大長了,不要超過12個字"  
  45. tooShortError="輸入太短了,不能少於4個字" />   
  46.   
  47. <mx:HTTPService id="login" url="http://localhost/myphp/flex4/src/login.php" useProxy="false" showBusyCursor="true"  
  48.    resultFormat="text" method="POST" result="login_success(event)" fault="login_fault(event)">   
  49. <mx:request>   
  50.    <username>{username.text}</username>   
  51.    <password>{password.text}</password>   
  52. </mx:request>   
  53.   
  54. </mx:HTTPService>   
  55.   
  56. <mx:Panel x="10" y="10" width="369" height="273" layout="absolute" id="loginPanel" title="登錄" fontSize="14" fontWeight="normal">   
  57.    <mx:Form x="51" y="10" width="276" height="196" label="訪問登錄" id="form1" defaultButton="{submit}">   
  58.       <mx:FormHeading label="歡迎訪問登錄"/>   
  59.     <mx:FormItem label="用戶名" required="true" fontSize="12">   
  60.      <mx:TextInput id="username" maxChars="20"/>   
  61.     </mx:FormItem>   
  62.     <mx:FormItem label="密碼" fontSize="12">   
  63.      <mx:TextInput id="password" maxChars="20"/>   
  64.     </mx:FormItem>   
  65.     <mx:FormItem width="200" fontFamily="Arial" fontSize="12" direction="horizontal">   
  66.           
  67.         <mx:Button label="提交" id="submit" click="onSubMit(event);"/>   
  68.         <mx:Button label="重寫" id="reset" click="onReSet(event);"/>   
  69.     </mx:FormItem>   
  70.    </mx:Form>   
  71. </mx:Panel>   
  72. <mx:TextArea x="532" y="10" id="conShow" width="210" height="77" wordWrap="true"/>   
  73.   
  74. </mx:Application>  

 

 

返回數據的格式resultFormat 有幾種類型,object、array、xml、flashvars、text和e4x,默認的設置爲object。
RESULT_FORMAT_ARRAY : String = "array"
[] 結果格式“array”與“object”相似,但是其返回的值始終爲數組;這樣,如果從結果格式“object”返回的結果尚不是數組,則將把該項目添加爲一個新數組的第一個項目。
RESULT_FORMAT_E4X : String = "e4x"
[] 結果格式“e4x”指定返回的值是一個 XML 實例,此實例可以使用 ECMAScript for XML (E4X) 表達式訪問。
RESULT_FORMAT_FLASHVARS : String = "flashvars"
[] 結果格式“flashvars”指定返回的值是包含由 & 符號分隔的名稱=值 對的文本,該文本被分析爲 ActionScript 對象。
RESULT_FORMAT_OBJECT : String = "object"
[] 結果格式“object”指定返回的值是 XML,但按照 ActionScript 對象樹分析。
RESULT_FORMAT_TEXT : String = "text"
[] 結果格式“text”指定 結果文本應爲未經處理的字符串。
RESULT_FORMAT_XML : String = "xml"
[] 結果格式“xml”指定結果應作爲指向父 flash.xml.XMLDocument 的第一個子項的 flash.xml.XMLNode 實例返回。
================================================
在 MXML 文件中使用 <mx:HTTPService> 標籤代表 HTTPService 對象。當調用 HTTPService 對象的 send() 方法時,將發出對指定 URL 的 HTTP 請求,並且返回 HTTP 響應。可以選擇向指定 URL 傳遞參數。如果沒有使用基於服務器的代理服務,則只能使用 HTTP GET 或 POST 方法。如果將 useProxy 屬性設置爲 true 並使用基於服務器的代理服務,則還可以使用 HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章