问题描述:
Vue请求后端资源报错
Access to XMLHttpRequest at ‘http://127.0.0.1:8080’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
原因分析:
跨域报错
解决方案:
在springboot中的WebMvcConfigurer类中添加跨域方法
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true).maxAge(3600);
}