【发布时间】:2017-01-11 11:12:02
【问题描述】:
我正在创建一个使用 jQuery 编写的 Web 前端,它将 REST 请求发送到使用 Spring 框架用 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 中,但没有任何改变。有人知道如何解决这个问题吗?
谢谢你, 马可
【问题讨论】:
标签: java jquery spring google-chrome cors