angular - Cross Origin 如何在 Spring Boot 和 Angular Cli 之间工作?
问题描述
我正在关注本教程Spring/Angular
当我运行我的应用程序时,我收到此错误:
从源“ http://localhost:4200 ”访问“ http://localhost:8080/api/employees ”处的 XMLHttpRequest已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头出现在请求的资源。
我对 Cross Origin 提出了不同的问题,但我不知道在哪里插入它以及在上面插入什么,因为在教程中从未显示或使用它。
解决方案
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class CORSConfiguration {
@SuppressWarnings("deprecation")
@Bean
public WebMvcConfigurer corsConfigurer()
{
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "DELETE", "OPTIONS");
}
};
}
}
推荐阅读
- swift - 带有自定义初始化的 SwiftUI 视图
- php - 如何在 if(isset) 语句中使用 PHP 获取 URL 参数以更新表单
- php - Laravel 将数据传递到视图中
- c - issue with the using print function unable to print string
- java - 使用带有 GTFS 数据的协议缓冲区时无法克服“无效的线路类型”
- python - 如何以最小的时间间隔有效地找出两个数据帧中的共同元素?
- bash - bash脚本中没有空格的变量赋值背后的基本原理是什么
- c++ - 如何使用 OpenACC 对微分方程进行积分?
- node.js - 如何获取网站的 Node.js 源代码?
- javascript - 创建角色,然后将角色赋予成员不起作用,有没有更好的方法来处理这个问题?不和谐.js