1.创建corsConfig 配置文件
@Configuration
public class corsConfig {
@Autowired
varModel varModel_;
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
//重写父类提供的跨域请求处理的接口
public void addCorsMappings(CorsRegistry registry) {
//添加映射路径
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins(varModel_.getCorsStrList());
}
};
}
}//end
2.varModel中的跨域设置
3.前端页面ajax请求 页面域名,http://localhost:8001
$.ajax({
type: "POST",
url: "http://localhost:8000/a/b",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({
"requestId": ""
}),
success: function (d) {
console.log(d);
},
error: function () {
//alert("系统错误");
}
});
4.参考链接
跨域设置:
https://spring.io/guides/gs/rest-service-cors/
http://www.spring4all.com/article/177
https://www.jianshu.com/p/55643abe7a18
支持CORS跨域浏览器列表:
https://caniuse.com/#feat=cors