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中的跨域设置

springboot webapi 支持跨域 CORS

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

 

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2021-09-17
  • 2021-05-19
  • 2022-12-23
猜你喜欢
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2021-08-01
  • 2021-06-11
相关资源
相似解决方案