如何从 Grails 中的 AJAX 错误回调中检索错误消息?
我正在发送以下 AJAX 请求:
$.ajax({ type: 'POST',
data: $(this).parents('form:first').serialize(), url:'/myapp/comment/saveOrUpdate',
success: function(data,textStatus) {insertNewComment(data)},
error: function(xhr, textStatus, errorThrown) {alert(xhr.responseText);}
})
...我的控制器操作如下所示:
class CommentController {
...
def saveOrUpdate = {
...
if (error) {
response.sendError 419, 'You must wait at least 5 minutes before posting a new comment'
}
}}
不幸的是,我从未在 HTTP 响应中看到我发送的消息。相反,我收到了一个由 Tomcat/Apache(或 Grails?)生成的错误 HTML 页面,如下所示:
<html><head><title>Apache Tomcat/6.0-snapshot - Error report</title>
</head><body><h1>HTTP Status 419 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>Cannot find message associated with key http.419</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0-snapshot</h3></body></html>
如何绕过生成的错误 HTML 页面,以便从 JavaScript 代码中检索消息“您必须等待...”?
感谢您的帮助。
I am sending the following AJAX request :
$.ajax({ type: 'POST',
data: $(this).parents('form:first').serialize(), url:'/myapp/comment/saveOrUpdate',
success: function(data,textStatus) {insertNewComment(data)},
error: function(xhr, textStatus, errorThrown) {alert(xhr.responseText);}
})
...and my controller action looks like this :
class CommentController {
...
def saveOrUpdate = {
...
if (error) {
response.sendError 419, 'You must wait at least 5 minutes before posting a new comment'
}
}}
Unfortunately, I never see the message I send in the HTTP response. Instead, I got an error HTML page generated from Tomcat/Apache (or Grails?) that looks like :
<html><head><title>Apache Tomcat/6.0-snapshot - Error report</title>
</head><body><h1>HTTP Status 419 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>Cannot find message associated with key http.419</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0-snapshot</h3></body></html>
How can I bypass the generated error HTML page in order to retrieve from the JavaScript code the message 'You must wait...' ?
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在设置 HTTP 状态错误,但实际上您正在捕获业务逻辑问题——应用程序错误。为什么不直接设置适当的应用程序级状态代码和消息(您自己设计的),并在 ajax 响应中返回它们(包括剩余时间)?如果依赖 HTTP 错误代码,则可能会在不同的浏览器中得到不同的结果,其中一些浏览器会在错误页面小于 512 字节时劫持错误消息。
在您引用的情况下,Apache 找不到与 419 匹配的已知错误消息。
您可以将一条添加到您的
.htaccess
配置中:...然后使该 419.html 页面显示您喜欢的任何内容(并确保该页超过 512 字节)。但我敢打赌,使用应用程序级状态代码正是您所寻找的。
You're setting an HTTP status error, but really you're catching a business-logic problem -- an application error. Why not just set up an appropriate application-level status code and message (of your own design), and return those in the ajax response (including time-remaining)? If you rely on HTTP error codes, you may get different results in different browsers, some of which hijack the error message if the error page is less than 512 bytes.
In the case you cite, Apache finds no known error message matching 419.
You could add one to your
.htaccess
config:...and then make that 419.html page say whatever you like (and ensuring the page exceeds 512 bytes). But I'd bet working with application-level status codes is what you're looking for.