gpt4 book ai didi

java - 从小程序调用javascript方法时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:01 25 4
gpt4 key购买 nike

我正在尝试使用 netscapte.java.JSObject 从 Applet 调用 javascript 方法。

在小程序中:

JSObject window = JSObject.getWindow(this.Class); 
Object[] args = .... //arguments
window.call("javascriptMethodName", args);

但是我在 window.call 得到了异常:

JavaScript error while calling "callFromJava"netscape.javascript.JSException: JavaScript error while calling "callFromJava"    at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)    at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)    at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)    at TextBoxApplet.jButton1_actionPerformed(TextBoxApplet.java:57)    at TextBoxApplet.access$000(TextBoxApplet.java:16)    at TextBoxApplet$1.actionPerformed(TextBoxApplet.java:36)    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)    at java.awt.Component.processMouseEvent(Unknown Source)    at javax.swing.JComponent.processMouseEvent(Unknown Source)    at java.awt.Component.processEvent(Unknown Source)    at java.awt.Container.processEvent(Unknown Source)    at java.awt.Component.dispatchEventImpl(Unknown Source)    at java.awt.Container.dispatchEventImpl(Unknown Source)    at java.awt.Component.dispatchEvent(Unknown Source)    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)    at java.awt.Container.dispatchEventImpl(Unknown Source)    at java.awt.Component.dispatchEvent(Unknown Source)    at java.awt.EventQueue.dispatchEvent(Unknown Source)    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)    at java.awt.EventDispatchThread.run(Unknown Source)

JSObject 不是null。有没有人遇到过这个?多谢。

最佳答案

我做了一个快速测试,我的发现与 musicfreak 非常吻合的评论。

使用以下小程序:

public class MyClass extends JApplet {
public void init() {
JSObject window = JSObject.getWindow(this);
Object[] args = new String[] { "bar" };
window.call("foo", args);
}
}

和以下标记(注意错误的 JavaScript - 在我传递的 bar 参数中没有 baz 方法):

<html>
<head>
<title>MyAppletTest</title>
</head>
<body>
<script type="text/javascript">
function foo(bar) { bar.baz(); }
</script>
<applet code="MyApplet.class"></applet>
</body>
</html>

我得到错误:

netscape.javascript.JSException: JavaScript error while calling "foo"
at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
at MyApplet.init(MyApplet.java:13)

例如,如果我通过将 bar.baz() 替换为 alert(bar) 来修复我的 JavaScript 函数,一切正常。

长话短说 - 再看一下您尝试调用的 JavaScript 函数,以及您的 Applet 在调用时传递的参数。

关于java - 从小程序调用javascript方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3058815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com