[英]Access is Denied Exception when calling JavaScript function from Java in IE
I am getting Access Denied exception when I am calling a Java Script Function from Java Applet only in IE. 仅在IE中从Java Applet调用Java脚本函数时,出现“拒绝访问”异常。 ( modified my original question with updated information.) (使用更新的信息修改了我的原始问题。)
Here is my HTML code 这是我的HTML代码
<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. 在同一页面中全局声明ju
,该页面从其他JavaScript文件中调用doneUpload。 I have included MAYSCRIPT in my applet Tag. 我已将MAYSCRIPT包含在我的applet标签中。
Java Code [After adding AccessController] : 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;
}
It throws an JSException: Access is denied
抛出JSException: Access is denied
The alert
function in uploadComplete
is not called. 没有调用uploadComplete
的alert
功能。 What am I doing wrong here? 我在这里做错了什么?
A summary from that answer on the Sun forums: Sun论坛上该答案的摘要:
Hello, I had same problem. 您好,我遇到了同样的问题。 I solved it, you just need to implement applets method like this: 我解决了它,您只需要实现以下applet方法:
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. 这种方法的唯一问题是,当您第一次调用method时,它需要花费相当长的时间来响应(4-5s),而下一次,响应是立即的。
Edit : OK, a second potential solution is to use getDocument() as described in this article . 编辑 :好的,第二种可能的解决方案是使用本文所述的getDocument()。 One word of warning, in our testing, we have seen that this is not always reliable on Firefox on the Mac. 一句话警告,在我们的测试中,我们已经看到这在Mac上的Firefox上并不总是可靠的。 The essence of this method is to do: 该方法的本质是:
getAppletContext().showDocument(new URL("javascript:uploadComplete()"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.