在 IE 中从 Java 调用 JavaScript 函数时出现访问被拒绝异常

发布于 2024-08-14 07:10:56 字数 1566 浏览 3 评论 0原文

当我仅在 IE 中从 Java Applet 调用 Java 脚本函数时,出现访问被拒绝异常。 (用更新的信息修改了我原来的问题。)

这是我的 HTML 代码

<script type="text/javascript">
   function uploadComplete() {
       alert("in Upload Complete");
       ju.doneUpload(true);
   }
</script>

ju 在同一页面中全局声明,该页面从不同的 JavaScript 文件调用 didUpload。我已将 MAYSCRIPT 包含在我的小程序标签中。

Java 代码 [添加 AccessController 后] :

 AccessController.doPrivileged(new PrivilegedAction<Object>() {
 public Object run() {

JSObject scriptObject = getScriptObject(uploadDialogBox.applet);
if(scriptObject != null) {
    try {
        // this is the call where it   throws an exception
       **scriptObject.call("uploadComplete", null);**
    } catch(JSException e) {
        System.out.println("exception " + e.getMessage() 
            + " WrappendException " + e.getWrappedException()
            + " stack trace " + e.getStackTrace());
    }
}
return null;
}

});

private JSObject getScriptObject(JApplet appletInstance) {
    JSObject result = null;
    // JSObject doc = null;
    try {
        result = JSObject.getWindow(appletInstance);
        // doc = (JSObject) result.getMember("document");
    } catch (JSException e) {
        System.out.println("Exception in getScriptObject : " + e.getMessage()
            + " Wrappend exception " + e.getWrappedException());
    }
    return result;
}

它抛出 JSException:访问被拒绝

未调用 uploadComplete 中的 alert 函数。我在这里做错了什么?

I am getting Access Denied exception when I am calling a Java Script Function from Java Applet only in IE. ( modified my original question with updated information.)

Here is my HTML code

<script type="text/javascript">
   function uploadComplete() {
       alert("in Upload Complete");
       ju.doneUpload(true);
   }
</script>

ju is declared globally in the same page which calls the doneUpload from a different JavaScript file. I have included MAYSCRIPT in my applet Tag.

Java Code [After adding AccessController] :

 AccessController.doPrivileged(new PrivilegedAction<Object>() {
 public Object run() {

JSObject scriptObject = getScriptObject(uploadDialogBox.applet);
if(scriptObject != null) {
    try {
        // this is the call where it   throws an exception
       **scriptObject.call("uploadComplete", null);**
    } catch(JSException e) {
        System.out.println("exception " + e.getMessage() 
            + " WrappendException " + e.getWrappedException()
            + " stack trace " + e.getStackTrace());
    }
}
return null;
}

});

private JSObject getScriptObject(JApplet appletInstance) {
    JSObject result = null;
    // JSObject doc = null;
    try {
        result = JSObject.getWindow(appletInstance);
        // doc = (JSObject) result.getMember("document");
    } catch (JSException e) {
        System.out.println("Exception in getScriptObject : " + e.getMessage()
            + " Wrappend exception " + e.getWrappedException());
    }
    return result;
}

It throws an JSException: Access is denied

The alert function in uploadComplete is not called. What am I doing wrong here?

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

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

发布评论

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

评论(1

满意归宿 2024-08-21 07:10:56

有帮助吗?

Sun 论坛上的回答摘要:

您好,我遇到了同样的问题。我解决了,你只需要实现这样的小程序方法:

public void f() {
   AccessController.doPrivileged(new PrivilegedAction<Object>() {
      public Object run() {
         // do something
         return null;
      }
   });
}

这种方法的唯一问题是,当你第一次调用方法时,需要很长时间才能响应(4-5秒),并且每次调用时,响应是立即。

编辑:好的,第二个可能的解决方案是使用 getDocument(),如 这篇文章。需要警告的是,在我们的测试中,我们发现这在 Mac 上的 Firefox 上并不总是可靠。这个方法的本质是做:

getAppletContext().showDocument(new URL("javascript:uploadComplete()"));

Does this help?

A summary from that answer on the Sun forums:

Hello, I had same problem. I solved it, you just need to implement applets method like this:

public void f() {
   AccessController.doPrivileged(new PrivilegedAction<Object>() {
      public Object run() {
         // do something
         return null;
      }
   });
}

The only problem with this approach is that when you call method for the first time, it takes pretty long time to respond (4-5s), and every next time, response is immediately.

Edit: OK, a second potential solution is to use getDocument() as described in this article. One word of warning, in our testing, we have seen that this is not always reliable on Firefox on the Mac. The essence of this method is to do:

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