Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .error jqXHR.responseText is empty

Tags:

jquery

I have a Java Spring MVC controller that can throw an exception. I have an @ExceptionHandler setup to handle these errors, and I'd like to use it to return the exception's message to the caller.

The server code is:

    @ExceptionHandler(DeviceException.class)
    @ResponseStatus(HttpStatus.METHOD_FAILURE)
    @ResponseBody
    public String handleException(DeviceException ex)   {
      return ex.getMessage();
    } 

I have tested throwing a DeviceException and getting the result using Curl from the CLI, the exception's message is being returned in the response body. I can't get the jQuery .error handler to display it however, my handler code is:

            error: function(jqXHR, textStatus, errorThrown) {
              // problem, the data is always blank
              alert('jqXHR.responseText = ', jqXHR.responseText);
            }

All I ever get for the responseText is an empty string. How do I get the string returned by the @ExceptionHandler to display in the jQuery error handler?

like image 682
fred basset Avatar asked Jul 27 '11 20:07

fred basset


1 Answers

Try something like this:

var msg = '<div style="padding:20px;">';
msg += "We're sorry, there was an error during your request.  Please refresh the page, then try again.  If your problem persists, please contact technical support. <br /><br /> <b>Cause:</b> <br /> ";
switch (textStatus) {
    case ('timeout'):
        msg += 'Request timed out.';
        break;
    case ('error'):
        msg += 'Error status returned.';
        break;
    case ('abort'):
        msg += 'Request was aborted.';
        break;
    case ('parseerror'):
        msg += 'There was an error parsing your request.';
        break;
    default:
        msg += 'No status returned.';
        break;
}
$(this).html(msg + ' <br /><br /> ' + errorThrown + ' <br /></div>');
like image 82
MaddHacker Avatar answered Nov 08 '22 04:11

MaddHacker