我正在创建一个使用jQuery编写的Web前端,它使用
Spring框架将REST请求发送到用
Java编写的REST Web服务.我在CORS请求中遇到了一个奇怪的错误.特别是,如果我使用Safari,一切正常;相反,使用Chrome,所有DELETE请求都失败(GET和POST请求在Chrome中也能正常工作).
我得到的错误是:
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.
当Chrome发出第一个OPTIONS请求时会发生这种情况.
我关于CORS的代码是:
@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();
}
}
我还尝试将@CrossOrigin注释添加到我的@RestController,但没有任何改变.有人知道要解决这个问题吗?
谢谢,
马尔科