title: Access to XMLHttpRequest has been blocked by CORS policy in ASP.NET CORE
body: <p>I tried to make connection from my side client to the API Server with ajax, but I get a error in the response :
  <strong><em>Access to XMLHttpRequest at '<a href="https://localhost:44381/api/webinars" rel="noreferrer">https://localhost:44381/api/webinars</a>' from origin '<a href="http://localhost:5003" rel="noreferrer">http://localhost:5003</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.</em></strong>
  I tried to edit the CROS policy in the API but I still have the same error.
   This is my ajax code from MVC:</p>
  
  <pre><code> $.ajax({
              url: "https://localhost:44381/api/webinars",
              type: 'POST',
              headers: {
                  contentType: "application/json",
              },
              body: JSON.stringify(body),
              success: function (data) {
                  console.log(data);
              },
              failure: function (err) {
                  console.log(err);
              }
  
          });
  </code></pre>
  
  <p><strong>StartUp from API:</strong></p>
  
  <pre><code>  public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
  
          services.AddCors(options =&gt;
          {
              options.AddPolicy("AllowMyOrigin",
              builder =&gt; builder.WithOrigins(
                  "http://localhost:5003/",
                  "http://apirequest.io")
                  .WithMethods("POST","GET","PUT")
                  .WithHeaders("*")
                  );
          });
            //code
      }
  
     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          //code
          app.UseCors("AllowMyOrigin");
          app.UseHttpsRedirection();
          app.UseMvc();
      }
  }
  </code></pre>
  
  <p><strong>Controller in the API:</strong></p>
  
  <pre><code>[EnableCors("AllowMyOrigin")]
  
  [Route("api/[controller]")]
  [ApiController]
  public class WebinarsController : ControllerBase
  {
          // POST: api/Webinars
      [HttpPost]
      public async Task &lt;ActionResult&gt; PostAsync([FromBody] Item newItem)
      {     
           //code
        }
   }
  </code></pre>
  
  <p>Thanks for the answer.</p>