views:

62

answers:

1

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.

A: 

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:

ErrorDocument 419 /errordocs/419.html

...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.

Ken Redler
I have implemented an application-level status codes as you have suggested.Thank you.
fabien7474