zoukankan      html  css  js  c++  java
  • springboot webapi 支持跨域 CORS

    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

    欢迎指正:haizi2014@qq.com
  • 相关阅读:
    面向对象三大特征------多态
    接口的概念
    面向对象(抽象类)
    面向对象三大特征------继承
    面向对象三大特征------封装
    成员变量和局部变量的区别
    20180822 ajax post 方式请求
    20180815 权限
    20180815 视图
    20180814 夜晚小目标
  • 原文地址:https://www.cnblogs.com/hcfan/p/10126146.html
Copyright © 2011-2022 走看看