【发布时间】:2021-04-02 03:08:07
【问题描述】:
我在结合 Angular 的 Spring Boot 项目中遇到 CORS 问题。
我正在使用以下代码并将其注释为配置。
@Configuration
public class WebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowedOrigins("http://localhost:4200");
}
};
}
}
在 Angular 中我添加了这个:
不幸的是,我仍然收到此错误:
他们都没有完成这项工作。
【问题讨论】:
-
CORS需要在服务器端进行管理,因此将这些标头客户端添加到您的请求不会有任何影响。 -
尝试将 OPTIONS 添加到允许的方法中
-
不,很遗憾没有。
-
Please don't post screenshots of text。它们无法被搜索或复制,并且可用性差。相反,将代码作为文本直接粘贴到您的问题中。如果选择它并单击
{}按钮或Ctrl+K,则代码块将缩进四个空格,这将导致其呈现为代码。
标签: angular spring-boot cors