title: Spring ResponseEntity & AJAX error function: can't access response body content
body: <p><strong>SERVER SIDE: Spring Framework</strong></p>
  
  <p>I have a <em>Spring Controller</em> that has a method that returns the type <code>ResponseEntity&lt;String&gt;</code>.</p>
  
  <p>For completely good requests I return the following:</p>
  
  <pre><code>return new ResponseEntity&lt;&gt;(OK_MESSAGE, new HttpHeaders(), HttpStatus.OK);
  </code></pre>
  
  <p>But if there's any problem during the execution or Exception catching, I return:</p>
  
  <pre><code>return new ResponseEntity&lt;&gt;(ERROR_MESSAGE, new HttpHeaders(), HttpStatus.BAD_REQUEST);
  </code></pre>
  
  <p>Where <code>ERROR_MESSAGE</code> contains a customized String for each type of Exception catched.</p>
  
  <p><strong>CLIENT SIDE: AJAX call</strong></p>
  
  <p>When that POST method is called and returns <em>HttpStatus.OK</em>, AJAX </p>
  
  <pre><code>success: function (data, message, xhr)
  </code></pre>
  
  <p>is called and I can easilly access the String <code>OK_MESSAGE</code> by accessing <code>data</code>.</p>
  
  <p>The <strong>problem</strong> comes that POST method returns <em>HttpStatus.BAD_REQUEST</em>, AJAX </p>
  
  <pre><code>error: function (xhr, status, errMsg)
  </code></pre>
  
  <p>is called but I cannot access the String <code>ERROR_MESSAGE</code>sent by the Server, which I need to show the user.</p>
  
  <p>Any suggestions?</p>