flash – 如何修复此跨域ActionScript 3错误?
我将尽可能具体和冗长,并包括我正在使用的一些代码.我已经进行了搜索,找到了
this question,看起来很相似;但是作者使用的是ActionScript 2而不是3,而我似乎无法有效地应用任何给出我自己情况的答案.
我试图通过Flash / ActionScript 3模拟(以有限的方式)JavaScript的XMLHttpRequest对象的行为,以克服相同的域限制.但我发现ActionScript在这方面有其自身的局限性.我承认我可能会弄错,但据我所知,理论上仍然可以使用ActionScript进行这种跨域脚本编写,只要你获得所有权限.那就是我遇到麻烦的地方. 首先,我为一个名为AjaxRequest的类借用了一些开源代码,我将其保存为/ajax/AjaxRequest.as.然后我创建了一个名为/jsajax.fla的Flash文件,该文件导出到最终的SWF文件/jsajax.swf.现在,这是包含Flash文件的第一个也是唯一一个框架的ActionScript代码: import ajax.AjaxRequest; Security.allowDomain("domainone.com"); Security.allowDomain("domaintwo.com"); function jsAjax(stringURL:String,stringMethod:String,stringData:String):void { var xhr:AjaxRequest = new AjaxRequest(stringURL); xhr.contentType = "application/x-www-form-urlencoded"; xhr.dataFormat = URLLoaderDataFormat.TEXT; if ( stringMethod.toUpperCase() == "POST" ) { xhr.method = URLRequestMethod.POST; } else { xhr.method = URLRequestMethod.GET; } xhr.addEventListener("complete",jsAjaxResponse); xhr.send(stringData); return; } function jsAjaxResponse(evt:Event):void { ExternalInterface.call("jsAjaxResponse",evt.currentTarget.data.toString()); return; } ExternalInterface.addCallback("jsAjax",jsAjax); ExternalInterface.call("jsAjaxReady"); 到现在为止还挺好.我感觉不需要一个或多个Security.allowDomain调用,但他们尝试解决这个问题是我的(不成功)尝试. 在我的JavaScript中,我定义了三个函数:jsAjax,jsAjaxResponse和jsAjaxReady.最后一个是一个非常基本的函数,用于指示Flash对象成功加载(仅在加载时立即调用一次),而另外两个用于发送和接收数据.如您所见,它们具有相应的ActionScript对应物. 最后,我创建了一个名为/test.html的简单HTML页面,它嵌入了这个SWF文件(使用swfobject),并有几个简单的表单控件来调用jsAjax函数.我的所有JavaScript定义也都嵌入在这个HTML文件中.我还创建了一个非常简单的PHP脚本,名为/test.php,它打印出$_REQUEST数组的内容.这是我打算使用这个ajax方法请求的脚本. 我测试了三种情况,但我只能让其中两种工作: 情景一:所有在一台服务器上 domainone.com/jsajax.swf domainone.com/test.html domainone.com/test.php 再次,这是有效的. jsAjaxResponse函数接收来自test.php的数据就好了. 场景二:在两台服务器上,向左倾斜 domainone.com/jsajax.swf domainone.com/test.html domaintwo.com/test.php domaintwo.com/crossdomain.xml 在crossdomain.xml文件中明确允许domainone.com时,这可行.同样,jsAjaxResponse函数接收来自test.php的数据就好了. 情景三:在两台服务器上,向右倾斜 domainone.com/test.html domaintwo.com/jsajax.swf domaintwo.com/test.php domaintwo.com/crossdomain.xml 这是我唯一无法工作的情况,这就是我需要开始工作的情况.前两个实际上只是测试场景,看看脚本是否正常工作.当我尝试在这里运行我的jsAjax函数时,我最终会在Firebug中出现两次错误: uncaught exception: Error calling method on NPObject! [plugin exception: Error in Actionscript. Use a try/catch block to find error.]. uncaught exception: Error calling method on NPObject! [plugin exception: Error in Actionscript. Use a try/catch block to find error.]. 救命!如何在场景3中使用它? 解决方法
我只想出来了!它确实与我的Flash文件中的Security.allowDomain命令有关.我实际上是在domainone.com的子域上托管test.html,这就是抛弃它.它必须是完全匹配,子域和所有.事实证明,我不需要引用domaintwo.com的Security.allowDomain命令,也不需要在场景3中完全存在crossdomain.xml文件!
因为在我最终使用的这个脚本的“真实”版本中,我不一定知道domainone.com实际上是什么,我将Flash文件顶部的代码更改为: import ajax.AjaxRequest; try { var domain1:String = LoaderInfo(this.root.loaderInfo).parameters["allow"]; if ( domain1.length > 0 ) { Security.allowDomain(domain1); } } catch (error:Error) { } try { var domain2:String = LoaderInfo(this.root.loaderInfo).parameters["allowhttps"]; if ( domain2.length > 0 ) { Security.allowInsecureDomain(domain2); } } catch (error:Error) { } ... 然后,在我用来嵌入SWF的JavaScript中,我基本上从document.location.toString()获取页面的当前域,检查它是否使用http或https,然后传递域在flashvars参数中作为allow和/或allowhttps.可能有一种“更好”的方法,不依赖于在Flash Vars中明确设置域(从Flash中进行某种自动检测),但我对ActionScript的精通程度不足以确定出.由于这个文件已经在JavaScript和ActionScript之间进行了大量的双向通信,因此这并不是什么大问题.这个解决方案对我来说足够好了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |