Velocity - 使用jQuery时如何避免ParseErrorException? [英] Velocity - How to avoid ParseErrorException when using jQuery?
问题描述
我正在尝试在网页上的某些JavaScript中添加jQuery帖子。整个页面由几个Velocity模板构成。一切都很好,直到我尝试添加jQuery帖子,现在我得到:
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:
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~~~
第282行是 $ .post(...
,第24列似乎是第一个,字符。最初我在这上面有JSON但我把它向上移动(到 var myJSONObject ...
行)因为我认为与无效JSON相关的错误(行开头的标签给出误导列号)。
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).
var myJSONObject = {"body": "", "action": "postcomment", "submitted": "true", "ajax": "true"};
myJSONObject.body = $("body").val();
$.post("$!{articleurl}", myJSONObject, function(result){
btn.textContent='Comment sent successfully.';
});
次要更新
我更改了以下行:
var url = "$articleurl";
$.post(url, myJSONObject, function(result){
~~~snip~~~
解析异常仍然关注第一个,。我假设问题是Velocity认为它应该能够解析$ .post - 事实上,它是jQuery。我在其他Velocity VM模板中使用了jQuery而没有任何问题。有没有办法让Velocity在解析时忽略某些行/语句?
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?
更新2
我发现此链接,但它无法解决我的问题。添加 \ 之前 $。post
给出了完全相同的错误,但是该列是一个额外的,因为在行的开头添加了字符。
I found this link about 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.
推荐答案
你可以用包装你的javascript#[[ ...]]#
告诉Velocity不解析封闭的块(Velocity 1.7中的新内容)
You can wrap your javascript with #[[ ... ]]#
which tells Velocity to not parse the enclosed block (new in Velocity 1.7)
#[[
<script>
...
</script>
]]#
这篇关于Velocity - 使用jQuery时如何避免ParseErrorException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!