javascript Velocity - 使用 jQuery 时如何避免 ParseErrorException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6828342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Velocity - How to avoid ParseErrorException when using jQuery?
提问by Michael
I'm trying to add a jQuery post to some JavaScript on a web page. The entire page is built up of several Velocity templates. Everything has been fine until I've tried to add the jQuery post, now I get:
我正在尝试将 jQuery 帖子添加到网页上的某些 JavaScript 中。整个页面由几个 Velocity 模板组成。一切都很好,直到我尝试添加 jQuery 帖子,现在我得到:
org.apache.velocity.exception.ParseErrorException: Encountered "," at line 282, column 24 of /WEB-INF/velocity/www/comments.vm
Was expecting one of:
"(" ...
<RPAREN> ...
<ESCAPE_DIRECTIVE> ...
~~~snip~~~
Line 282 is $.post(...
and column 24 appears to be the first "," character. Initially I had the JSON on this line, but I moved it up (to the var myJSONObject ...
line)as I thought the error related to invalid JSON (tabs at the start of the line gave a misleading column number).
第 282 行是$.post(...
,第 24 列似乎是第一个“,”字符。最初我在这条线上有 JSON,但我将它向上移动(到var myJSONObject ...
线),因为我认为错误与无效的 JSON 相关(该行开头的选项卡给出了一个误导性的列号)。
var myJSONObject = {"body": "", "action": "postcomment", "submitted": "true", "ajax": "true"};
myJSONObject.body = $("body").val();
$.post("$!{articleurl}", myJSONObject, function(result){
btn.textContent='Comment sent successfully.';
});
Minor Update
次要更新
I changed the following lines:
我更改了以下几行:
var url = "$articleurl";
$.post(url, myJSONObject, function(result){
~~~snip~~~
The parse exception still focuses on the first ",". I'm assuming the issue is that Velocity thinks it should be able to resolve $.post - when in fact, it's jQuery. I've used jQuery in other Velocity VM templates without any problem. Is there a way to get Velocity to ignore certain lines / statements when parsing?
解析异常仍然集中在第一个“ ,”上。我假设问题是 Velocity 认为它应该能够解析 $.post - 事实上,它是 jQuery。我在其他 Velocity VM 模板中使用了 jQuery,没有任何问题。有没有办法让 Velocity 在解析时忽略某些行/语句?
Update 2
更新 2
I found this linkabout escaping references in Velocity, but it does not resolve my issue. Adding a "\" before $.post
gives me the exact same error, but the column is one extra, because of the character added at the start of the line.
我找到了有关在 Velocity 中转义引用的链接,但它没有解决我的问题。添加一个“ \”之前$.post
给了我完全相同的错误,但该列是额外的,因为在行的开头添加了字符。
回答by serg
You can wrap your javascript with #[[ ... ]]#
which tells Velocity to not parse the enclosed block (new in Velocity 1.7)
你可以用#[[ ... ]]#
它来包装你的 javascript,它告诉 Velocity 不要解析封闭的块(Velocity 1.7 中的新功能)
#[[
<script>
...
</script>
]]#
回答by Michael
Ok, there appears to be two solutions for this:
好吧,似乎有两种解决方案:
First, with jQuery we can just avoid using the global alias $ and instead use the jQuery object directly:
首先,使用 jQuery 我们可以避免使用全局别名 $ 而是直接使用 jQuery 对象:
jQuery.post(url, myJSONObject, function(result){
~~~snip~~~
In my case, the above works great. But I suspect in other scenarios (non-jQuery) this may not be possible. In which case, we can 'hide' our character within a valid Velocity reference like this:
就我而言,上述方法效果很好。但我怀疑在其他情况下(非 jQuery)这可能是不可能的。在这种情况下,我们可以像这样在有效的 Velocity 引用中“隐藏”我们的角色:
#set( $D = '$' )
${D}
Source: http://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
来源:http: //velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
I'd still like to know why the backslash escape didn't work, but the above will at least get me moving again. :)
我仍然想知道为什么反斜杠转义不起作用,但上面的内容至少会让我再次动起来。:)
回答by zhb
I think this is a bugin version 1.6.x, because it works fine in 1.7(If it did not, please tell me, I test it many times..), according to the reference, the $
takes effect only when it is followed by a-zA-Z
. I want to try do debug what happened really, but the translation code is generated by Java CC tool, it is too hard to recognize the logic...
我觉得这是1.6.x版本的BUG,因为在1.7中运行良好(如果不行请告诉我,我测试了很多次..),根据参考,$
只有在遵循时才生效由a-zA-Z
. 我想尝试调试真正发生的事情,但是翻译代码是Java CC工具生成的,逻辑太难识别了...
回答by giang.sys
you must create a js file with your javascript code and import your js file into your vm code
您必须使用您的 javascript 代码创建一个 js 文件并将您的 js 文件导入到您的 vm 代码中
回答by wonderbjorn
I couldn't get it to work with any of the other fixes like escaping "$" in velocity unfortunately. I got it working by loading an external js-file with the jQuery instead of writing jQuery directly in velocity. Worked out for me at least, hope it helps someone :)
不幸的是,我无法让它与任何其他修复程序一起使用,例如在速度中转义“$”。我通过使用 jQuery 加载外部 js 文件而不是直接以速度编写 jQuery 来实现它。至少对我有用,希望它可以帮助某人:)
/bj?rn
/bj?rn