title: $.ajax(...) POST to Jersey WebService, unable to get back HTTP error details (status code, etc)
body: <p>Here's my setup:</p>
  
  <p>Java REST web service (implemented with Jersey). I use JQuery to to HTTP GETs and POSTs towards it.</p>
  
  <p>My JQuery AJAX config is as follows:</p>
  
  <pre><code>$.ajaxSetup ({
          cache: false,
          error: function(jqXHR, textStatus, errorThrown) {
              console.log("=== error function ===");
              console.log("jqXHR.status=" + jqXHR.status);
              console.log("jqXHR.responseText=" + jqXHR.responseText);
              console.log("textStatus=" + textStatus);
              console.log("errorThrown=" + errorThrown);
          }
      });
  
  $(document).ajaxError(function(event, jqXHR, settings, exception ) {
      console.log("=== ajaxError function ===");
      console.log("settings.url=" + settings.url);
      console.log("jqXHR.status=" + jqXHR.status);
      console.log("jqXHR.responseText=" + jqXHR.responseText);
      console.log("exception=" + exception);
  });
  </code></pre>
  
  <p><strong>If I do a HTTP GET to an invalid URL:</strong> </p>
  
  <pre><code>$.getJSON("http://validServer/invalidAddress", 
          requestJSON, 
          function(responseJSON) {
              //...
          }); 
  </code></pre>
  
  <p>I get nice error details from both the "error" and "ajaxError" configurations:</p>
  
  <pre><code>=== error function ===
  jqXHR.status=404
  jqXHR.responseText=[web server 404 message]
  textStatus=error
  errorThrown=Not Found
  === ajaxError function ===
  settings.url=http://validServer/invalidAddress
  jqXHR.status=404
  jqXHR.responseText=[web server 404 message]
  exception=Not Found
  </code></pre>
  
  <p>Also, if I look in Chrome's Network inspector Tab I can see than an (unsuccesfull) HTTP GET was performed.</p>
  
  <p><strong>If I do a HTTP POST to an invalid URL:</strong> </p>
  
  <pre><code>$.ajax({
        url: "http://validServer/invalidAddress",
        type: "POST",
        data: JSON.stringify(requestJSON),
        dataType: "text",
        headers: {
          "content-type": "application/json",
          "Accept": "application/json"
        },
        success: function(responseJSON){
            //...
        }
  });
  </code></pre>
  
  <p><strike>First, it's pretty weird that when I look in Chrome's console, I can see that in fact an (unsuccesfull) HTTP OPTIONS was performed. I've noticed that even with valid JQuery ajax HTTP POSTs, Chrome's console reports that first an HTTP OPTIONS was made and <em>then</em> the HTTP POST.</strike>. This behavior is normal, as I am usign <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests" rel="nofollow">cross-site requests</a>.</p>
  
  <p>However, my main issue is that now, the output of the above error-handling code gives almost no details on the error:</p>
  
  <pre><code>=== error function ===
  jqXHR.status=0
  jqXHR.responseText=
  textStatus=error
  errorThrown=
  === ajaxError function ===
  settings.url=http://validServer/invalidAddress
  jqXHR.status=0
  jqXHR.responseText=
  exception= 
  </code></pre>
  
  <p><strong>My question is:</strong> </p>
  
  <p>How do I get from JQuery's Ajax POST the details of an error (HTTP Status Code, Message, etc), like I do when I do an Ajax GET ? </p>
  
  <p><strong>Update</strong> </p>
  
  <p>I should mention that the option of passing the error function immediately when I make the Ajax POST has the same issue. If I do it like this:</p>
  
  <pre><code>$.ajax({
        url: "http://validServer/invalidAddress",
        type: "POST",
        data: JSON.stringify(requestJSON),
        dataType: "text",
        headers: {
          "content-type": "application/json",
          "Accept": "application/json"
        },
        success: function(responseJSON){
            //...
        },
        error: function (jqXHR, textStatus, errorThrown) {
          console.log(jqXHR.status);
          console.log(textStatus);
          console.log(errorThrown);
        }
  });
  </code></pre>
  
  <p>The console output for the above code is the same as in my first example, no HTTP Status Code, no server error message, etc. </p>