我有
def exception = request.exception.stackTraceLines
在Groovy控制器中。如何在JavaScript中获取exception
的值。
答案 0 :(得分:1)
如果您要在退货时添加例外情况。
flash.message = message(exception: 'Error: xxx');
你可以像这样得到它
<div class="message" role="status"> ${flash.message} </div>
只需使用$ {your flash.your_var_name}
答案 1 :(得分:1)
您有多种方法可以解决此问题。使用javascript时,我通常使用一个可以保存错误消息/ stacktrace的包装器。这取决于您希望在何处以及如何进行错误处理。例如:
def book = new Book(params)
book.validate()
render (contentType: "text/json") {[
"data": book,
"errors": book.hasErrors() ? book.errors : null
]}
然后,您可以在返回JSON时检查“errors”是否有值,以确定输入中是否存在错误。 (Elvis运算符也可能有效,book.errors?:null)我通常在我的JavaScript中定义的错误回调中处理的其他(未捕获)异常。 (主要是jQuery,(在这种情况下使用malsup jquery.form.js))
$(function() {
$("form").live("submit", function() {
$(this).ajaxSubmit({
error: function (msg) {
/* Catch hard errors here (500's) */
alert("Error occurred: " + msg);
},
success: function(wrapper) {
if (wrapper.errors != null) {
/* Handle model errors here */
} else {
/* Parse data here */
var book = wrapper.data;
}
}
});
return false;
});
答案 2 :(得分:0)
你也可以这样做
render(template:"book",model:[book:theBook, exception:exception])
或者像这样(没有尝试,不知道是否有效)
render(template:"book",model:[book:theBook, exception:exception!=null?exception:""])
然后像这样从GSP访问
${exception.getMessage()}
希望它有所帮助。