title: How would you handle errors when using jQuery.ajax()?
body: <p>When using <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a>'s <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options" rel="nofollow noreferrer">ajax method</a> to submit form data, what is the best way to handle errors?
  This is an example of what a call might look like:</p>
  
  <pre><code>$.ajax({
      url: "userCreation.ashx",
      data: { u:userName, p:password, e:email },
      type: "POST",
      beforeSend: function(){disableSubmitButton();},
      complete: function(){enableSubmitButton();},
      error: function(xhr, statusText, errorThrown){
              // Work out what the error was and display the appropriate message
          },
      success: function(data){
              displayUserCreatedMessage();
              refreshUserList();
          }
  });
  </code></pre>
  
  <p>The request might fail for a number of reasons, such as duplicate user name, duplicate email address etc, and the ashx is written to throw an exception when this happens.</p>
  
  <p>My problem seems to be that by throwing an exception the ashx causes the <code>statusText</code> and <code>errorThrown</code> to be <strong>undefined</strong>.</p>
  
  <p>I can get to the <code>XMLHttpRequest.responseText</code> which contains the HTML that makes up the standard .net error page.</p>
  
  <p>I am finding the page title in the responseText and using the title to work out which error was thrown. Although I have a suspicion that this will fall apart when I enable custom error handling pages.</p>
  
  <p>Should I be throwing the errors in the ashx, or should I be returning a status code as part of the data returned by the call to <code>userCreation.ashx</code>, then using this to decide what action to take?<br>
  How do you handle these situations?</p>