美文网首页
springboot解决ajax跨域问题

springboot解决ajax跨域问题

作者: 山水风情 | 来源:发表于2017-07-06 20:01 被阅读0次

    首先新建一个类

    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;
    
    /**
     * @author 创建人:< >
     * @project 项目:<>
     * @date 创建时间:< 2017/7/6>
     * @comments: 说明:< 解决ajax跨域问题 >
     */
    
    @Configuration
    public class CorsConfig {
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig());
            return new CorsFilter(source);
        }
    }
    
    ajax请求成功

    相关文章

      网友评论

          本文标题:springboot解决ajax跨域问题

          本文链接:https://www.haomeiwen.com/subject/rcrkhxtx.html