Flex 跨域访问解决方案

想让swf 文件在一台服务器上运行并且能够访问另一台服务器上的资源,这时就会遇到跨域问题,请看下面的解决方案。

写一个名字为 crossdomain.xml 的XML文件,这个方法是允许任何Flash都可以调用,放到被调用文件的网站根目录下:
view plaincopy to clipboardprint?
<?xml version="1.0"?> 
<!DOCTYPE cross-domain-policy  
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
<cross-domain-policy> 
<allow-access-from domain="*" /> 
</cross-domain-policy> 
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>


只让你允许的网站来调用你的数据.
view plaincopy to clipboardprint?
<?xml version="1.0"?>   
<!DOCTYPE cross-domain-policy  
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
<cross-domain-policy> 
<allow-access-from domain="www.yoursite.com" /> 
<allow-access-from domain="yoursite.com" /> 
<allow-access-from domain="*.k-zone.cn" /> 
</cross-domain-policy> 
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="www.yoursite.com" />
<allow-access-from domain="yoursite.com" />
<allow-access-from domain="*.k-zone.cn" />
</cross-domain-policy>

如果使用XMLSocket要把端口号加上
view plaincopy to clipboardprint?
<cross-domain-policy> 
<allow-access-from domain="*.foo.com" to-ports="507,516" /> 
<allow-access-from domain="*.bar.com" to-ports="516-523" /> 
<allow-access-from domain="www.foo.com" to-ports="507,516-523" /> 
<allow-access-from domain="202.117.80.202" to-ports="*" /> 
</cross-domain-policy> 
<cross-domain-policy>
<allow-access-from domain="*.foo.com" to-ports="507,516" />
<allow-access-from domain="*.bar.com" to-ports="516-523" />
<allow-access-from domain="www.foo.com" to-ports="507,516-523" />
<allow-access-from domain="202.117.80.202" to-ports="*" />
</cross-domain-policy>


如果安全策略文件放在根目录,文件名字必须为crossdomain.xml,如果不放在根目录,
要用flash.system.security.loadPolicyFile("http://xcopy.net.cn/crossdomain.xml")
这样的方法加载安全策略文件。

http://localhost:8080/crossdomain.xml

如果以上还不能成功访问,请再住下看:
请注意代码中的:<param name="allowScriptAccess" value="always"/>
view plaincopy to clipboardprint?
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=512px height=384px 
      codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> 
      <param name="movie" value="http://www.xcopy.net.cn/test.swf" /> 
      <param name="quality" value="high" /> 
      <param name="allowScriptAccess" value="always"/>      
      <embed src="http://www.xcopy.net.cn/test.swf" quality="high" bgcolor="#869ca7" 
        width=512px height=384px name="/test" align="middle" 
        type="application/x-shockwave-flash" 
        allowScriptAccess="always" 
        pluginspage="http://www.adobe.com/go/getflashplayer"> 
      </embed> 
</object> 

 

注意:crossdomain.xml是放在服务器那边,而不是客户端这边.
如访问的数据来源为:192.168.0.2  监听端口:8080 
访问端口:192.168.0.1  监听端口:8080   只用把crossdomain.xml放在192.168.0.2上就行,使得
http://192.168.0.2/crossdomain.xml可以访问到

你可能感兴趣的:(Flex 跨域访问解决方案)