我该如何解决这个问题的跨域ActionScript 3的错误? [英] How do I fix this cross-domain ActionScript 3 error?

查看:224
本文介绍了我该如何解决这个问题的跨域ActionScript 3的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尽可能具体和详细越好,包括一些code我使用。我已经做了搜索,发现<一href="http://stackoverflow.com/questions/1038668/cross-domain-externalinterface-error-calling-method-on-npobject">this问题,这似乎是相似的;然而,使用ActionScript 2,而不是3的作者有,我似乎无法应用任何给我自己有效的情况下的答案。

I'm going to be as specific and verbose as possible and include some of the code I'm using. I already did a search and found this question, which seems similar; however the author there was using ActionScript 2 instead of 3, and I couldn't seem to apply any of the answers given to my own situation effectively.

我试图仿效(以有限的方式)JavaScript的XMLHtt prequest对象的行为通过Flash / ActionScript 3中,为了克服同域限制。但我发现,动作已经在这方面其自身的局限性。我承认,我可能是误会,但是从我的理解是理论上还是有可能做到使用ActionScript这种跨域脚本,只要你得到所有的权限权利。而这也正是我遇到麻烦了。

I am trying to emulate (in a limited way) the behavior of JavaScript's XMLHttpRequest object through Flash/ActionScript 3, in order to overcome the same-domain limitation. But I'm discovering that ActionScript has its own limitations in that regard. I admit that I might be mistaken, but from what I understand it is theoretically still possible to do this sort of cross-domain scripting using ActionScript, so long as you get all the permissions right. And that's where I'm running into trouble.

首先,我借了一些开源$ C ​​$ C为一类,称为 AjaxRequest ,我已经保存为 /ajax/AjaxRequest.as 。然后,我创建 /jsajax.fla 称为一个Flash文件,它出口到最终的SWF文件, /jsajax.swf 。现在,这里的动作code包含Flash文件的第一个也是唯一框架:

First, I borrowed some open-source code for a class called AjaxRequest, which I have saved as /ajax/AjaxRequest.as. I then created a Flash file called /jsajax.fla which exports to the final SWF file, /jsajax.swf. Now, here's the ActionScript code that comprises the first and only frame of the Flash file:

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 不需要调用的感觉,但他们是我的(失败的)尝试,试图解决这个问题。

So far so good. I have a feeling that one or more of those Security.allowDomain calls are not needed, but they were my (unsuccessful) attempts to try to solve this problem.

在我的JavaScript,我已经定义三大功能: jsAjax jsAjaxResponse jsAjaxReady 。最后一个是只是用来表明Flash对象成功加载(其仅在加载它调用一次,并立即)一个非常基本的功能,而其他两个是用于发送和接收的数据。正如你所看到的,他们有相应的动作对应。

In my JavaScript, I've got three functions defined: jsAjax, jsAjaxResponse, and jsAjaxReady. The last one is just a very basic function used to indicate that the Flash object loaded successfully (which is only called once and immediately upon loading it), while the other two are used for sending and receiving the data. As you can see, they have corresponding ActionScript counterparts.

最后,我创建了一个叫做一个简单的HTML页面 /test.html 嵌入该SWF文件(使用的swfobject ),并具有调用 jsAjax 函数几个简单的表单控件。我所有的JavaScript的定义嵌入在这个HTML文件了。我还创建了打印出 $ _ REQUEST 数组的内容被称为一个非常简单的PHP脚本 /test.php 。这就是我打算用这个AJAX方法来请求该脚本。

Finally, I created a simple HTML page called /test.html that embeds this SWF file (using swfobject) and has a couple of simple form controls for calling the jsAjax function. All of my JavaScript definitions are embedded in this HTML file too. I also created a very simple PHP script called /test.php that prints out the contents of the $_REQUEST array. That's the script I intend to request using this ajax method.

有三种方案,我测试过,但我只能得到其中的两个工作:

There are three scenarios that I tested, but I was only able to get two of them working:


场景一:所有的一台服务器
如果我上传的所有的这些文件,以domainone.com,然后请求test.php的,它工作正常。我的文件/文件夹结构,那么看起来是这样的:

SCENARIO ONE: All on one server
If I upload all of these files to domainone.com, and then request test.php, it works fine. My file/folder structure then looks like this:

domainone.com/jsajax.swf
domainone.com/test.html
domainone.com/test.php

此外,这部作品。该jsAjaxResponse函数接收回程从test.php的就好了数据。

Again, this works. The jsAjaxResponse function receives back the data from test.php just fine.


方案二:在两台服务器上,扶着左
当我上载的HTML和SWF到第一台服务器,然后只需使用它调用PHP脚本的第二个服务器上,它不工作的时候了。我做了一些挖掘,发现通过创建domaintwo.com的授权访问domainone.com一个的crossdomain.xml 文件,该固定它。所以,我的文件/文件夹结构是这样的:

SCENARIO TWO: On both servers, leaning left
When I uploaded the HTML and SWF to the first server, and then just have it call the PHP script on the second server, it didn't work right away. I did some digging, and found that by creating a crossdomain.xml file on domaintwo.com that granted access to domainone.com, this fixed it. So my file/folder structure looked like this:

domainone.com/jsajax.swf
domainone.com/test.html
domaintwo.com/test.php
domaintwo.com/crossdomain.xml

在明确允许domainone.com在crossdomain.xml文件,这部作品。同样,jsAjaxResponse函数接收回来的数据test.php的就好了。

When explicitly allowing domainone.com in the crossdomain.xml file, this works. Again, the jsAjaxResponse function receives back the data from test.php just fine.


方案三:在两台服务器上,倚权
当我上传所有,但HTML来domaintwo.com,我再也不能得到它的工作。换句话说,在domainone.com的HTML所引用托管在domaintwo.com一个SWF文件,以及SWF文件正在试图使一个请求domaintwo.com。我离开了相同的crossdomain.xml文件从场景2,以防万一。我的文件/文件夹结构是这样的:

SCENARIO THREE: On both servers, leaning right
When I uploaded all but the HTML to domaintwo.com, I could no longer get it to work. In other words, the HTML on domainone.com is referencing an SWF file hosted on domaintwo.com, and that SWF file is trying to make a request to domaintwo.com. I left the same crossdomain.xml file from Scenario 2, just in case. My file/folder structure looked like this:

domainone.com/test.html
domaintwo.com/jsajax.swf
domaintwo.com/test.php
domaintwo.com/crossdomain.xml

这是我不能让工作的唯一情况,这是我需要工作的情况。前两个真的只是测试场景,看剧本工作的。当我试图在这里经营我jsAjax功能,我风与显示了两次Firebug的一个错误:

This is the only case I could not get working, and this is the case I need to get working. The first two were really just test scenarios to see if the script was working at all. When I try to run my jsAjax function here, I wind up with an error that shows up twice in 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个工作?

Help! How do I get it working in scenario 3?

推荐答案

我只是理解了它!它确实有做在我的Flash文件中的的Security.allowDomain 命令。我其实是主持的test.html 上domainone.com的子域,那就是把它扔了。它必须完全匹配,子域和所有。原来我并不需要的Security.allowDomain 命令引用domaintwo.com,也没有我需要的的crossdomain.xml 文件是present在所有的方案3!

I just figured it out! It did have to do with the Security.allowDomain commands in my Flash file. I was actually hosting the test.html on a subdomain of domainone.com, and that was throwing it off. It has to be an exact match, subdomain and all. It turns out I didn't need the Security.allowDomain command that referenced domaintwo.com, nor did I need the crossdomain.xml file to be present at all for Scenario 3!

由于在真实的版本这个剧本,我最终使用的,我不一定会知道什么domainone.com实际上是,我改变了code对Flash文件到该的顶部:

Since in the "real" version of this script that I end up using, I won't necessarily know what domainone.com actually is, I changed the code on the top of the Flash file to this:

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) { }
...

然后,在JavaScript中我使用嵌入到SWF那里摆在首位,我基本上抓住页从当前域document.location.toString(),检查它是否是使用HTTP或HTTPS,然后将域名作为允许和/或 allowhttps 的参数的FlashVars参数。有可能是一个更好的方式来做到这一点,不依赖于Flash变量(某种自动检测从Flash)设置的域了明确的,但我不会在ActionScript不够好图是精通出。而且,由于该文件已经做了一大堆的JavaScript和ActionScript之间的双向沟通​​,这不是什么大不了的事了。该解决方案是好足够了。

Then, in the JavaScript I'm using to embed the SWF there in the first place, I basically grab the current domain of the page from document.location.toString(), check whether it's using http or https, and then pass the domain in as allow and/or allowhttps in the flashvars parameter. There might be a "better" way to do this that doesn't rely on setting the domain up explicitly in the Flash Vars (some kind of automatic detection from within Flash), but I'm not versed in ActionScript well enough to figure that out. And since this file is already doing a whole bunch of bidirectional communication between JavaScript and ActionScript, it's not that big of a deal. This solution is good enough for me.

这篇关于我该如何解决这个问题的跨域ActionScript 3的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆