I am creating a web front written using jQuery that sends a REST request to a Java REST web service using a Spring framework. I am facing a weird bug with CORS requests. In particular, everything works fine if I use Safari; instead, with Chrome, all DELETE requests fail (both GET and POST requests work fine in Chrome too).
The error I am getting is:
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access.
The response had HTTP status code 403.
This happens when the first OPTIONS request is issued by Chrome.
My CORS related code:
@Configuration
@EnableWebMvc
public class CorsConfigurator extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS");
}
}
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new CorsConfigurator();
}
}
I also tried adding annotations @CrossOriginto mine @RestController, but nothing changed. Anyone know to fix this?
Thanks Marco