今天用Flex調用ASP.NET的Web Service時老是跳出了"Security error accessing url"的錯誤,查了資料,才知道這原來根Flash的沙箱有關。
原理是,當SWF檔案試圖訪問其它域上的資料時,Flash Player自動載入那個域上的policy檔案,如果SWF檔案所在的域被包含在這個policy檔案中,資料就可以被訪問,否則就不行,以下是Adobe官方的原文:
When a SWF file attempts to access data from another domain, Flash Player automatically attempts to load a policy file from that domain. If the domain of the SWF file that is attempting to access the data is included in the policy file, the data is automatically accessible.
而我所碰到的問題,就是因為我的IIS上沒有這個policy file,那解決辦法自然就是加上這個policy file。
解決辦法:
建立一個xml檔案,命名為crossdomain.xml,如果不想麻煩的話,檔案名稱就不要用其它的,就用這個。
把以下內容複寫到xml檔案中:<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
然後把它儲存到wwwroot下面,我曾試著把它放到應用程式的根目錄,但不起作用,放到wwwroot下是可以的,上述的crossdomail.xml 允許所有的swf檔案訪問本主機。
然後就OK了。如果想看更詳細的,就看:
http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=05B_Security_176_04.html