title: Handling error in ajax request in Select2
body: <p>I need to detect if user session is gone when expanding Select2 combobox. When that condition occurs, response is redirected to login page.</p>
  
  <p>The Select2 is populated using an ajax call, so I have added this to the "ajax" parameter:</p>
  
  <pre><code>        transport: function (params, success, failure) {
                  var $request = $.ajax(params);
  
                  $request.then(success);
                  $request.fail(function (jqXHR, textStatus, errorThrown) {
                      alert(errorThrown);
                  });
  
                  return $request;
              }
  </code></pre>
  
  <p>The problem is that the error reported is not in XHR format (in order to detect 401 HTTP code) but an error telling "SyntaxError: Unexpected end of JSON input".</p>
  
  <p>When seeing the response using Chrome developer tools, I do see that the response was in XHR format with 401 HTTP code but Select2 transforms it in some way.</p>
  
  <p>Is there a way to solve this?</p>
  
  <p>Regards
  Jaime</p>