zoukankan      html  css  js  c++  java
  • ajax跨域-springboot

    package com.xxxx.xx.service.configuration;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    
    @Configuration
    public class CorsConfig {
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
            corsConfiguration.addAllowedHeader("*"); // 2允许任何头
            corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig()); // 4
            return new CorsFilter(source);
        }
    }  

    设置跨域自动加载

    Html请求代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>跨域请求</title>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            $.ajax({url:"http://alilive.ylzpay.cn:1400/oms/supIndex/getAreaOrderCount.ajax",success:function(result){
                $("#p1").html(result);
            }});
        });
    });
    </script>
    </head>
    <body>
    
    <p width="500px" height="100px" id="p1"></p>
    <button>获取其他内容</button>
    </body>
    </html>
  • 相关阅读:
    node中fs模块
    node生成excel,动态替换表格内容
    Postgresql存放数组形式的数据
    ubuntu下安装typescript
    随笔6
    excel文件导出相应数据统计内容
    随笔4
    随笔3.2
    随笔2
    随笔1
  • 原文地址:https://www.cnblogs.com/jevon/p/10683032.html
Copyright © 2011-2022 走看看