title: CORS error while calling Spring Boot Rest API from Ajax
body: <p>We are running Spring boot apps on ubuntu server. We are using Netflix Zuul as API Gateway and Eureka as Service Registry. And used Apache2 as webserver for DNS.</p>
  
  <p>When I am calling the APIs from my local via jQuery Ajax, I am getting the following exception:</p>
  
  <pre><code>Access to XMLHttpRequest at 'https://*****.com/api/users' from origin 'null' 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.
  </code></pre>
  
  <p>And I can see <strong>"Origin: null"</strong> under <strong>Request Headers</strong></p>
  
  <p><strong>Ajax code:</strong></p>
  
  <pre><code>$.ajax({ 
      type: "GET",
      dataType: "json",
      crossDomain: true,
      headers: {
          'Accept':'application/json',
          'Content-Type':'application/json',
          'Access-Control-Allow-Origin': "*"
      },
      url: "https://*****.com/api/users",
      success: function(data){        
          //alert(data);
          console.log(data);
      }
  });
  </code></pre>
  
  <p>To fix CORS tried following ways:
  Added required config in apache:</p>
  
  <pre><code>Header set Access-Control-Allow-Origin "*"
  Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
  Header set Access-Control-Max-Age "1000"
  Header set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
  </code></pre>
  
  <p>Added CorsMappings in WebMvcConfigurerAdapter implementation class:</p>
  
  <pre><code>@Override
  public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE")
              .allowedHeaders("*");
  }
  </code></pre>
  
  <p>Added CorsFilter bean in Zuul proxy class (annotated with @EnableZuulProxy):</p>
  
  <pre><code>@Bean
  public CorsFilter corsFilter() {
      final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      final CorsConfiguration config = new CorsConfiguration();
      config.setAllowCredentials(true);
      config.addAllowedOrigin("*");
      config.addAllowedHeader("*");
      config.addAllowedMethod("OPTIONS");
      config.addAllowedMethod("HEAD");
      config.addAllowedMethod("GET");
      config.addAllowedMethod("PUT");
      config.addAllowedMethod("POST");
      config.addAllowedMethod("DELETE");
      config.addAllowedMethod("PATCH");
      source.registerCorsConfiguration("/**", config);
      return new CorsFilter(source);
  }
  </code></pre>
  
  <p>But still same error. Any thoughts to fix this issue ?
  Thanks.</p>