title: How to return to AJAX error function from application_error?
body: <p>My code below ends up in the AJAX <code>success</code> function. Why? It should execute the <code>error</code> function. What am I doing wrong? </p>
  
  <pre><code>$.ajax({
      url: url,
      type: "POST",
      data: data,
      contentType: "application/json; charset=utf-8",
      success: function(data) {
          if (callback)
              callback(data);
  
          $.LoadingOverlay("hide");
      },
      error: function (event, jqxhr, settings, thrownError) {
          var t = "";
  
      }
  });
  </code></pre>
  
  <pre class="lang-csharp prettyprint-override"><code>protected void Application_Error(object sender, EventArgs e)
  {
      var ctx = HttpContext.Current;
  
      var exception = ctx.Server.GetLastError();
  
      bool isAjaxCall = string.Equals("XMLHttpRequest", Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);
      Context.ClearError();
      if (isAjaxCall)
      {
          //Context.Response.ContentType = "application/json";
          Context.Response.StatusCode = 200;
          Context.Response.Write(
              new JavaScriptSerializer().Serialize(
                  new { error = exception.Message }
              )
          );
      }
  }
  </code></pre>
  
  <p>The controller simply throws an exception:</p>
  
  <pre><code>throw new Exception("faulty");
  </code></pre>