在Flex利用 HttpService 和.net傳值

摘要:在Flex利用 HttpService 和.net傳值

方法一. 回傳結果為XML
 
  1.元件
 
<mx:HTTPService id="GetURL" 
url           = "http://localhost/SL/"
result        = "GetURLResult(event)" 
fault         = "GetURLFault(event)"
resultFormat  = "e4x"
method        = "POST"
showBusyCursor= "true"/> 

 

 
2.傳送httpservice 
 這個寫法 GetURL 用 POST或GET傳送均可
//用post/get均可
var flexPost:Object=new Object();
flexPost.flexURL=encodeURI(”看要傳什麼值”);
GetURL.send(flexPost);

 

 
 寫法二只能用GET傳送
var sURL:String="";
sURL="http://localhost/SL?flexURL="+encodeURIComponent("看要傳什麼值");
GetURL.url=sURL;
GetURL.send();

 

 
3. 接收回傳值
//==========================================================================
private function GetURLResult(event:ResultEvent):void
{
Alert.show(event.result.toXMLString());
}
//==========================================================================
private function GetURLFault(event:FaultEvent):void
{
Alert.show(event.message.toString());
}

 

 
方法二. 用text做為回傳結果
  這時候1.和3.需修正如下
<mx:HTTPService id="GetURL" 
url           = "http://localhost/SL/"
result        = "GetURLResult(event)" 
fault         = "GetURLFault(event)"
resultFormat  = "text"
method        = "POST"
showBusyCursor= "true"/> 

 

private function GetURLResult(event:ResultEvent):void
{
         Alert.show(event.result.Result.URL.toString());

}

 

 
 
補充
如果一直遇到以下的錯誤訊息
 
(mx.messaging.messages::ErrorMessage)#0
  body = (null)
  clientId = "DirectHTTPChannel0"
  correlationId = "2A67A0E3-F0B8-9EE4-EB48-09E69283C384"
  destination = ""
  extendedData = (null)
  faultCode = "Channel.Security.Error"
  faultDetail = "Destination: DefaultHTTP"
  faultString = "Security error accessing url"
  headers = (Object)#1
    DSStatusCode = 0
  messageId = "D78E913E-90A9-3803-59BC-09E692BE0334"
  rootCause = (flash.events::SecurityErrorEvent)#2
    bubbles = false
    cancelable = false
    currentTarget = (flash.net::URLLoader)#3
      bytesLoaded = 0
      bytesTotal = 0
      data = (null)
      dataFormat = "text"
    errorID = 2048
    eventPhase = 2
    target = (flash.net::URLLoader)#3
    text = "Error #2048"
    type = "securityError"
  timestamp = 0
  timeToLive = 0

 

就是遇到跨網域的問題,這時請在訪問站台下的根目錄增加crossdomain.xml文件,文件内容為:
<?xml vaersion="1.0"?>
<cross-domain-policy>
 <allow-access-from domain="*"/>
</cross-domain-policy>