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>
  • 相关阅读:
    寒假学习第九天
    寒假学习第八天
    寒假学习第七天
    寒假学习第六天
    寒假学习第五天
    寒假学习第四天
    寒假学习第三天
    寒假学习第二天
    寒假学习第一天
    阅读笔记
  • 原文地址:https://www.cnblogs.com/jevon/p/10683032.html
Copyright © 2011-2022 走看看