跨域外部接口“在 NPObject 上调用方法时出错”

发布于 2024-07-25 04:14:50 字数 764 浏览 4 评论 0原文

我正在尝试通过跨域的ExternalInterface 启用Javascript 和Flash 之间的通信。 当 Javascript 与 SWF 位于同一域时,效果非常好。 但在一种情况下,HTML 驻留在域 A 上,javascript 和 flash 都驻留在域 B 上。我已完成以下所有操作:

  • 嵌入标记具有 allowScriptAccess="always" (并且对象将其作为参数)
  • 我的 SWF 文件的 actionscipt 具有 Security.allowDomain("*")
  • 我的 SWF 还调用 Security.allowInsecureDomain("*")
  • 域 A 和域 B 有一个 /crossdomain.xml 文件,其中包含 allow-access-from domain="*"

SWF 能够在页面上调用 javascript,但是当我使用Javascript 调用由ExternalInterface 公开的函数,我明白了

调用 NPObject 上的方法时出错! [插件异常:Actionscript 中出现错误。 使用 try/catch 块来查找错误。]

这是 ActionScript 2,因此 ExternalInterface.marshallExceptions 不可用。

I am trying to enable communication between Javascript and Flash via ExternalInterface across domains. The Javascript works great when it is located on the same domain as the SWF. But in one case, the HTML resides on domain A, the javascript and the flash both reside on domain B. I have done all of the following:

  • The embed tag has allowScriptAccess="always" (and the object has that as a param)
  • My SWF file's actionscipt has Security.allowDomain("*")
  • My SWF also calls Security.allowInsecureDomain("*")
  • Both domain A and domain B have a /crossdomain.xml file which has allow-access-from domain="*"

The SWF is able to call javascript on the page, but when I use Javascript to call functions exposed by ExternalInterface, I get

Error calling method on NPObject! [plugin exception: Error in Actionscript. Use a try/catch block to find error.]

This is ActionScript 2 so ExternalInterface.marshallExceptions is not available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街角迷惘 2024-08-01 04:14:50

您只需要做两件事即可实现此功能:

1) allowscriptaccess=always 将允许您的 swf 将内容发送到页面

2) System.security.allowDomain("yourhtmldomain.com" );

请注意,它是 AS2 中的 System.security.allowDomain() - 它与 AS3 或您上面编写的内容不同。

上面的数字 2 允许域 A 上的 html 页面调用域 B 上的 swf 中的内容。

你的js所在的域在这里并不重要,因为浏览器将其嵌入到domainA上,所以脚本在domainA中执行。

crossdomain.xml 主要仅用于加载远程文件,而您没有这样做,因此您可以根据需要将其删除。 (并且您可能不希望在您的主域上有一个包含 allow="*" 的 crossdomain.xml 文件,这是非常糟糕的做法)

You should only need two things for this to work:

1) allowscriptaccess=always will allow your swf to send stuff out to the page

2) System.security.allowDomain("yourhtmldomain.com");

Note that it's System.security.allowDomain() in AS2 - it's not the same as AS3 or what you have written above.

number 2 above allows the html page on domainA to call things in the swf on domainB.

The domain your js is hosted on won't matter here, since the browser embeds it on domainA, the script is executed in domainA.

crossdomain.xml is mainly only for loading remote files, which you aren't doing, so you can remove that if you like. (and you probably don't want to have a crossdomain.xml file with allow="*" sitting on your main domain, that's very bad practice)

夕色琉璃 2024-08-01 04:14:50

由于您要加载多个 swf,因此您可能需要在域 B 上加载的每个 swf 中包含安全设置。

您可能还需要具有适当安全设置的加载程序上下文。

import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.net.URLRequest;
import flash.net.URLLoader;

var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain, (Security.sandboxType == Security.REMOTE) ? SecurityDomain.currentDomain : null);
var l:Loader = new Loader();
l.load(new URLRequest("http://example.com/myswf.swf"), context);

Since you are loading multiple swfs, you may need to include the security settings in each of those swfs on domain B that are loaded.

You may also need a loader context with the appropriate security settings.

import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.net.URLRequest;
import flash.net.URLLoader;

var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain, (Security.sandboxType == Security.REMOTE) ? SecurityDomain.currentDomain : null);
var l:Loader = new Loader();
l.load(new URLRequest("http://example.com/myswf.swf"), context);
橘香 2024-08-01 04:14:50

对我来说有几个原因是(我正在使用 uploadify):

http 服务器没有权限将文件写入目的地
swfobject(flash)没有跨域访问

解决办法:
html 中的对象标记必须具有allowScriptAccess =“always”,它可以通过设置参数来完成,就像

$('#file_upload').uploadifySettings('scriptAccess', 'always')
than flash object must have:
import flash.system.Security;
Security.allowDomain('remotedomain.com'); 

可以通过使用此参数编译源代码来完成一样,我有这个,如果您需要它,请使用uploadify主题写信给我。
与远程服务器相比,Flash 包含在页面中,根 crossdamoin.xml 文件中必须包含以下内容:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

for me a few reason was (i'm using uploadify):

http server haven't permission to write file to destination
swfobject (flash) haven't cross domain access

solution:
object tag in html must have allowScriptAccess="always" it can be done by set param like

$('#file_upload').uploadifySettings('scriptAccess', 'always')
than flash object must have:
import flash.system.Security;
Security.allowDomain('remotedomain.com'); 

it can be done by compile source with this param, i have that, if you need it write to me with uploadify subject.
Than Remote server, where flash include in the page, must have in the root crossdamoin.xml file with content like:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
晚雾 2024-08-01 04:14:50

我遇到了同样的问题(allowDomain 等都很好),但我发送到 flash 的错误参数 - 只是从 ajax 调用输出 JSON。 当我将该 json 放入“”中,然后将其解析为 javascript 对象(通过 jQuery.parseJSON)时,问题就消失了。

I had this same problem (allowDomain etc. were good), but I send to flash bad parameter - just outputed JSON from ajax call. Problem gone, when I put that json in "", and then parse it into javascript object (via jQuery.parseJSON).

長街聽風 2024-08-01 04:14:50

将 AS3 与 Flash Player 版本 10 一起使用时,我无法使 ExternalInterface.addCallback() 正常工作以进行本地测试。 我终于通过添加值为“all”的参数“allowNetworking”来使本地副本正常工作(http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/ wwhelp.htm?context=LiveDocs_Parts&file=00001079.html)。 祝所有为此苦苦挣扎的人好运!

Using AS3 with Flash Player version 10 I could not get ExternalInterface.addCallback() to work correctly for testing locally. I finally got my local copy working by adding the parameter "allowNetworking" with a value of "all" (http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001079.html). Good luck to anyone struggling with this!

橘寄 2024-08-01 04:14:50

就我而言,这是因为我正在修改包含上传器 div 的 DOM 元素。

我使用 jquery hide() 函数隐藏包含上传器的 div,当我意识到这会导致上述错误时,我尝试了另一种方法,设置 div 的“float”属性。 在这两种情况下,它都破坏了上传器。

FWIW,看来将包含上传器的 div 的宽度/高度设置为 0 不会导致错误发生。

In my case, it was because I was modifying the DOM element containing the uploader div.

I used the jquery hide() function to hide the div containing the uploader, and when I realized that caused the above error, I tried a different approach where I set the "float" attribute of the div. In both cases, it broke the uploader.

FWIW, it appears that setting the width/height of the div containing the uploader to 0 does NOT make the error occur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文