後端 (C# ASMX) 定義回傳類別
public class ReturnResult {
public int curIndex { get; set; }
public DataTable idxList { get; set; }
}
[WebMethod(Description = "依照片名取得連結所有資料")]
public ReturnResult getLinkDTByImgName(string strTb, string strImgName) {
ReturnResult rr = new ReturnResult();
if (conectDB()) {
rr.curIndex = Convert.ToInt32(getInfoByImgName(strTb, strImgName).Rows[0][0]);
string linkGuid = getInfoByImgName(strTb, strImgName).Rows[0][1].ToString();
string sql = "SELECT * FROM dbo." + strTb + " WHERE LinkGuid LIKE '%" + linkGuid + "%'";
var da = new SqlDataAdapter(sql, conn);
var ds = new DataSet();
da.Fill(ds);
rr.idxList = ds.Tables[0];
return rr;
}
return null;
}
Flex (MXML) 呼叫端
<s:Application xmlns:fx="https://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable] public var arr:ArrayCollection = new ArrayCollection();
public var idx:int;
protected function init():void {
myWeb.getLinkDTByImgName("PanView", "20131019072740-0000150");
}
protected function onResult(event:ResultEvent):void {
idx = event.result.curIndex;
arr = event.result.idxList.Tables.Table.Rows; // DataTable 序列化為巢狀結構
Alert.show(idx + " rows:" + arr.length);
}
]]>
</fx:Script>
<fx:Declarations>
<s:WebService id="myWeb"
wsdl="https://10.19.1.55/serverPano/WebServiceDuke.asmx?WSDL"
fault="Alert.show(event.fault.toString())">
<s:operation name="getLinkDTByImgName" result="onResult(event)"/>
<s:WebService>
</fx:Declarations>
</s:Application>
關鍵點
- ASMX 自動將 `DataTable` 序列化為巢狀物件:`result.idxList.Tables.Table.Rows` 為陣列。
- Flex 需在 `` 宣告 `` 並繫結 `operation` 的 `result` 事件。
- 跨網域需在伺服器根目錄放置 `crossdomain.xml` 允許存取。