我正在尝试从我的 java 类中调用 javascript。请在下面找到我的 java 代码:
File file = new File(".....js");
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByExtension("js");
engine.put("engine", engine);
// evaluate JavaScript code from String
try{
engine.eval(new java.io.FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的 javascript 包含一个美元符号 '$' ...所以,一旦我运行我的程序,我就会收到这个错误:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "$" is not defined. (<Unknown source>#4) in <Unknown source> at line number 4
'$' 是否未定义为 javascript 代码的一部分?脚本引擎不识别它吗?谢谢你的帮助。
这是我更新的脚本:
<script type="text/javascript" src="scripts/jquery.js"></script>;
<script type="text/javascript" src="scripts/jquery-ui.js"></script>;
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js">
$.ajax({
type: 'POST',
url: "../ManageChannel",
data: "_country="+country+"&_featured="+featured+"&_webviewLink="+webviewLink+"&_name="+locationName,
success:function(data)
{
if (data==2)
alert("RSS link is required");
else{
alert("Channel is successfully created!");
filePath=[];
window.location.href=window.location.href;
}
}});