title: jQuery Ajax error handling, show custom exception messages
body: <p>Is there some way I can show custom exception messages as an alert in my jQuery AJAX error message?</p>
  <p>For example, if I want to throw an exception on the server side via <a href="http://en.wikipedia.org/wiki/Apache_Struts" rel="noreferrer">Struts</a> by <code>throw new ApplicationException(&quot;User name already exists&quot;);</code>, I want to catch this message ('user name already exists') in the jQuery AJAX error message.</p>
  <pre><code>jQuery(&quot;#save&quot;).click(function () {
    if (jQuery('#form').jVal()) {
      jQuery.ajax({
        type: &quot;POST&quot;,
        url: &quot;saveuser.do&quot;,
        dataType: &quot;html&quot;,
        data: &quot;userId=&quot; + encodeURIComponent(trim(document.forms[0].userId.value)),
        success: function (response) {
          jQuery(&quot;#usergrid&quot;).trigger(&quot;reloadGrid&quot;);
          clear();
          alert(&quot;Details saved successfully!!!&quot;);
        },
        error: function (xhr, ajaxOptions, thrownError) {
          alert(xhr.status);
          alert(thrownError);
        }
      });
    }
  });
  </code></pre>
  <p>On the second alert in the error callback, where I alert <code>thrownError</code>, I am getting <code>undefined</code> and the <code>xhr.status</code> code is <code>500</code>.</p>
  <p>I am not sure where I am going wrong. What can I do to fix this problem?</p>