title: How to parse JSON web server reponse when HTTP code is an error?
body: <h2>The setup</h2>
  
  <p>In a web application I 'm maintaining there are lots of AJAX calls to submit forms that go like this:</p>
  
  <pre><code>jQuery.post($form.attr("action"), $form.serialize())
      .always(commonProcessing)
      .done(function() {
          // the post succeeded, do something specific (e.g. close a dialog)
      });
  </code></pre>
  
  <p>The <code>commonProcessing</code> function looks for "well known" parts in the JSON response and takes appropriate action (the response may also include request-specific information). It reads something like this:</p>
  
  <pre><code>function commonProcessing(result, outcome) {
      switch (outcome) {
          case "success":
              if (result.notify) {
                  // show a growl-style notification
              }
              else if (...) ;
              break;
          case "error":
              // show a generic "unexpected error" notification
              break;
      }
  }
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>In many cases I 'd prefer to return an HTTP error code -- say 400 for a submitted form that has invalid data -- because, in addition to this being the natural order of things, it allows me to conveniently put the "successful request" code in a <code>.done</code> handler (if a 200 OK were returned instead then the content would be always parsed, but the <code>.done</code> handler would need to try and find out if this is an "error" or a "really OK" response). At the same time I want to respond to the request with specific information when an error occurs (for example the <code>notify</code> object can contain an error message that explains exactly what was wrong with the request).</p>
  
  <p>The problem is that when an HTTP error code is returned, jQuery does not attempt to parse the response content, in which case the <code>result</code> argument is a jqXHR object instead of the typical parsed JSON.</p>
  
  <p>Is there an elegant way of saying "I know that there was an error, but I also want you to parse the response content"? I could of course do <code>$.parseJSON(result.responseText)</code> since we 're talking about JSON, but that feels clumsy because it sidesteps all the nice <a href="https://github.com/jquery/jquery/blob/master/src/ajax.js#L798" rel="nofollow">jQuery code</a> that sniffs the content type and does the parsing.</p>