title: How to make jQuery parse my error response represented as a valid json?
body: <p>Here is my code:</p>
  
  <pre><code>      $.ajax({
          url: "/api/invoice/" + newInvoice._id,
          type: 'PUT',
          data: JSON.stringify(newInvoice),
          dataType: 'json',
          contentType: "application/json; charset=utf-8"
        })
          .success(function () {
            $('#statusLine').text('Successfully submitted invoice {0}. Click here to dismiss.'.format(newInvoice._id));
          })
          .error(function (err) {
            alert(err);
          });
  </code></pre>
  
  <p>The request:</p>
  
  <pre><code>PUT http://localhost:8000/api/invoice/16211 HTTP/1.1
  Host: localhost:8000
  Connection: keep-alive
  Content-Length: 770
  Origin: http://localhost:8000
  X-Requested-With: XMLHttpRequest
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
  Content-Type: application/json; charset=UTF-8
  Accept: application/json, text/javascript, */*; q=0.01
  Referer: http://localhost:8000/invoice.html?id=16211
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  
  {"items":[{"id":...
  </code></pre>
  
  <p>The request body is actually a valid json, I have just truncated it for brevity.</p>
  
  <p>The response:</p>
  
  <pre><code>HTTP/1.1 409 Conflict
  X-Powered-By: Express
  Content-Type: application/json; charset=utf-8
  Content-Length: 1386
  ETag: 250542419
  Connection: keep-alive
  
  {
    "msg": "Cannot update the invoice #16211, because it has already been updated by someone else.",
    "invoice": {
      "items": [
        {...
  </code></pre>
  
  <p>Again, the response is a completely valid json, truncated for brevity. </p>
  
  <p>As expected, the <code>error</code> handler is invoked with the <code>err</code> object. However, how can I get hold on the parsed json? Of course, I could check that the content-type of the response is json and then parse the <code>err.responseText</code> myself, but isn't it what jQuery ajax is supposed to do for me? I mean, it does so for my <code>$.get</code> queries when I get the objects from the server.</p>
  
  <p>What am I missing?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is a correction to <a href="https://stackoverflow.com/a/12310751/80002">https://stackoverflow.com/a/12310751/80002</a>:</p>
  
  <p>Doing the request:</p>
  
  <pre><code>var ajax = $.ajax(...
  </code></pre>
  
  <p>Processing the error response:</p>
  
  <pre><code>var res, ct = ajax.getResponseHeader("content-type") || '';
  if (ct.indexOf('json') &gt; -1) {
    res = $.parseJSON(err.responseText);
    // process the response here
  }
  </code></pre>