test1.html:1 Access to XMLHttpRequest at '后端接口地址' from origin '前端地址'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.
二,解决方法
1.关于解决Springboot前后端分离项目的跨域问题
1.1基于WebMvcConfigurer配置的跨域
1.2创建Cors类,加入以下代码块
@Configuration
public class Cors implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
.allowCredentials(true).maxAge(3600);
}
}
2.此处注意springboot版本,本人使用的版本号是2.5.5
2.1在Springboot2.0.0中做如下配置
@Configuration
public class Cors implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
.allowCredentials(true).maxAge(3600);
}
}
一,浏览器控制台出现的问题:test1.html:1 Access to XMLHttpRequest at '后端接口地址' from origin '前端地址' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.二,解决方法1.关于解决Springboot前后端分离项目的跨域问题1.1基于WebMvcCo.