zoukankan      html  css  js  c++  java
  • SpringBoot支持AJAX跨域请求

     

    利用注解的方式解决AJAX请求跨域问题

    1.编写一个支持跨域请求的 Configuration

    - 第一种方式

    - CorsConfig.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    /**
    * 处理AJAX请求跨域的问题
    * @author Levin
    * @time 2017-07-13
    */
    @Configuration
    public class CorsConfig extends WebMvcConfigurerAdapter {
    static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS)
    .maxAge(3600);
    }
    }

    2.HTTP请求接口

    - HelloController.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @RestController
    public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String query() {
    return "hello";
    }
    }

    - 第二种方式(推荐)

    PS:第一种存在一个问题,当服务器抛出 500 的时候依旧存在跨域问题

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    @SpringBootApplication
    @ComponentScan
    @EnableDiscoveryClient
    public class ManagementApplication {

    public static void main(String[] args) {
    SpringApplication.run(ManagementApplication.class, args);
    }

    private CorsConfiguration buildConfig() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedMethod("*");
    corsConfiguration.addExposedHeader(HttpHeaderConStant.X_TOTAL_COUNT);
    return corsConfiguration;
    }

    /**
    * 跨域过滤器
    *
    * @return
    */
    @Bean
    public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", buildConfig()); // 4
    return new CorsFilter(source);
    }
    }

    - index.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <!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://localhost:8080/test",success:function(result){
    $("#p1").html(result);
    }});
    });
    });
    </script>
    </head>
    <body>

    <p width="500px" height="100px" id="p1"></p>
    <button>获取其他内容</button>
    </body>
    </html>
  • 相关阅读:
    Python 多线程与多进程
    Python3 Scrapy 安装方法
    吴恩达深度学习笔记 course4 week4 测验
    吴恩达深度学习笔记 course4 week 4 特殊应用:人脸识别与神经风格转换
    吴恩达深度学习笔记 course4 week3 测验
    吴恩达深度学习笔记 course4 week1 作业2
    吴恩达深度学习笔记 course4 week3 目标检测
    吴恩达深度学习笔记 course4 week2 作业1
    吴恩达深度学习笔记 course4 week2 测验
    DreamWeaver使用小结
  • 原文地址:https://www.cnblogs.com/lywJ/p/10711454.html
Copyright © 2011-2022 走看看