从另一个域访问swf

时间:2010-10-20 07:14:59

标签: flex actionscript-3 flash external loading

当尝试从不同于我的swf域的域加载swf时,我收到此错误:

*** Security Sandbox Violation ***
SecurityDomain 'file:///C:/Documents and Settings/Welcome/My Documents/Flex Builder 3/SwfLoad/bin-debug/SwfLoad.swf' tried to access incompatible context  

    'http://example.com/crossdomain.xml'
loading of class failed. class name is MGroundTileInsideZ1 failure error is SecurityError:  
 Error #2119: Security sandbox violation: caller file:///C:/Documents and Settings/Welcome/My Documents/Flex Builder 3/SwfLoad/bin-debug/SwfLoad.swf   
cannot access LoaderInfo.applicationDomain owned by http://example.com/zoom_assets/GroundTiles.swf.

这是我的crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" secure="false"/>
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>

这是我的动作代码:

package {
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.system.Security;

    public class SwfLoad extends Sprite
    {
        public function SwfLoad()
        {
            Security.loadPolicyFile("http://example.com/crossdomain.xml");
            var loader:Loader = new Loader();
            var url:String = "http://example.com/zoom_assets/GroundTiles.swf";
            var request:URLRequest = new URLRequest(url);
            var context:LoaderContext = new LoaderContext();
            context.checkPolicyFile = true;
            context.applicationDomain = ApplicationDomain.currentDomain;
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
                    function(e:Event):void {
                        try {
                            var className:String = "MGroundTileInsideZ1";
                           var appDomain:ApplicationDomain = loader.contentLoaderInfo.applicationDomain;
                            var cl:Class = appDomain.getDefinition(className) as Class;
                            //var cl:Class = getDefinitionByName(className) as Class;
                        }
                        catch (e:Error) {
                             trace("loading of class failed. class name is " + className + " failure error is " + e);
                         }
                    });
            loader.load(request,context);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

context.securityDomain = SecurityDomain.currentDomain;

我也必须包括上面的那行...

事实上,我可以取消:

 Security.loadPolicyFile("http://mydomain.com/crossdomain.xml");

 context.checkPolicyFile = true;  

答案 1 :(得分:0)

尝试从本地Web服务器而不是文件系统执行SWF。 在Flash Builder中,转到项目的属性,然后选择“Flex构建路径”。更改Output文件夹和Output文件夹URL以匹配Web服务器的文档根目录及其相应的URL。

如果您仍需要从本地文件系统启动SWF,请尝试调整Flash Player's security settings并将本地目录添加到受信任的目录。