简体   繁体   English

如何使用Flash(AS3)从远程域调用Flex SWF?

[英]How do I call a Flex SWF from a remote domain using Flash (AS3)?

I have a Flex swf hosted at http://www.a.com/a.swf . 我在http://www.a.com/a.swf上有一个Flex swf。 I have a flash code on another doamin that tries loading the SWF: 我在另一个尝试加载SWF的doamin上有一个flash代码:

_loader = new Loader();
var req:URLRequest = new URLRequest("http://services.nuconomy.com/n.swf");
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderFinish);
_loader.load(req);

On the onLoaderFinish event I try to load classes from the remote SWF and create them: 在onLoaderFinish事件中,我尝试从远程SWF加载类并创建它们:

_loader.contentLoaderInfo.applicationDomain.getDefinition("someClassName") as Class

When this code runs I get the following exception 当此代码运行时,我得到以下异常

SecurityError: Error #2119: Security sandbox violation: caller http://localhost.service:1234/flashTest/Main.swf cannot access LoaderInfo.applicationDomain owned by http://www.b.com/b.swf.
    at flash.display::LoaderInfo/get applicationDomain()
    at NuconomyLoader/onLoaderFinish()

Is there any way to get this code working? 有没有办法让这段代码有效?

This is all described in The Adobe Flex 3 Programming ActionScript 3 PDF on page 550 (Chapter 27: Flash Player Security / Cross-scripting): 这些都在第550页的Adobe Flex 3编程ActionScript 3 PDF中进行了描述(第27章:Flash Player安全/跨脚本编写):

If two SWF files written with ActionScript 3.0 are served from different domains—for example, http://siteA.com/swfA.swf and http://siteB.com/swfB.swf —then, by default, Flash Player does not allow swfA.swf to script swfB.swf, nor swfB.swf to script swfA.swf. 如果使用ActionScript 3.0编写的两个SWF文件是从不同的域提供的 - 例如http://siteA.com/swfA.swfhttp://siteB.com/swfB.swf-则默认情况下,Flash Player不会允许swfA.swf脚本swfB.swf,swfB.swf脚本swfA.swf脚本。 A SWF file gives permission to SWF files from other domains by calling Security.allowDomain(). SWF文件通过调用Security.allowDomain()为来自其他域的SWF文件授予权限。 By calling Security.allowDomain("siteA.com"), swfB.swf gives SWF files from siteA.com permission to script it. 通过调用Security.allowDomain(“siteA.com”),swfB.swf从siteA.com获取SWF文件的脚本权限。

It goes on in some more detail, with diagrams and all. 它继续更详细,图表和所有。

You'll need a crossdomain.xml policy file on the server that has the file you load, it should look a something like this: 你需要一个包含你加载文件的服务器上的crossdomain.xml策略文件,它应该是这样的:

<?xml version="1.0"?>
<!-- http://www.foo.com/crossdomain.xml -->
<cross-domain-policy>
  <allow-access-from domain="www.friendOfFoo.com" />
  <allow-access-from domain="*.foo.com" />
  <allow-access-from domain="105.216.0.40" />
</cross-domain-policy>

Put it as crossdomain.xml in the root of the domain you're loading from. 将它作为crossdomain.xml放在要加载的域的根目录中。

Also you need to set the loader to read this file as such: 您还需要设置加载程序以读取此文件:

var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
loader.load( new URLRequest( "http://my.domain.com/image.png" ), loaderContext );

code sample yoinked from http://blog.log2e.com/2008/08/15/when-a-cross-domain-policy-file-is-not-enough/ 代码示例来自http://blog.log2e.com/2008/08/15/when-a-cross-domain-policy-file-is-not-enough/

Mayhaps System.Security.allowDomain is what you need? Mayhaps System.Security.allowDomain是你需要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM