title: How Can I bypass CORS to bubble an error back to the client after an ajax call
body: <p>I have an ajax call with a <code>done</code> and <code>fail</code> leg:</p>
  
  <pre><code>$.ajax({
              url: apiUrl + "/auth/sign-in",
              type: "POST",
              dataType: "text",
              data: loginFormData,
              contentType: "application/x-www-form-urlencoded",
              crossDomain: true,
              headers: {"Access-Control-Allow-Origin": "*"}
  
          })…..
  </code></pre>
  
  <p>and the fail function declared in the form <code>.fail(function (xhr, status, error) {</code></p>
  
  <p>In my startup.cs file when adding the CORS service, I'm setting it up in this way:</p>
  
  <pre><code>services.AddCors(options =&gt;
          {
              options.AddPolicy("CorsPolicy", builder =&gt;
                   builder.AllowAnyOrigin().AllowCredentials()
                  .WithMethods("GET", "POST", "HEAD")
                  .WithHeaders("Acccept", "Accept-Language", "Content-Language", "Content-Type", "Last-Event-ID")
                  .Build());
          });
  </code></pre>
  
  <p>In my Web Api Method I raise an error back:</p>
  
  <pre><code> Exception inner = new Exception { Source = "WebApi: Authorisation" };
   throw new UnauthorizedAccessException("You must login",inner);
  </code></pre>
  
  <p>the code drops back into the fail function of the ajax call but error is empty. In the Browser (Chrome) I get the following error:</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at '<a href="http://localhost:54981/auth/sign-in" rel="nofollow noreferrer">http://localhost:54981/auth/sign-in</a>' from origin '<a href="http://localhost:54979" rel="nofollow noreferrer">http://localhost:54979</a>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  </blockquote>