美文网首页我爱编程
typescript与springboot跨域请求

typescript与springboot跨域请求

作者: 纳尔没怒 | 来源:发表于2018-01-12 14:23 被阅读0次

    //在项目中添加跨域请求具体配置

    @Configuration

    public class CorsConfig {

        private CorsConfiguration buildConfig() {

            CorsConfiguration corsConfiguration = new CorsConfiguration();

            corsConfiguration.addAllowedOrigin("*");//设置访问源地址

            corsConfiguration.addAllowedHeader("*");//设置访问源请求头

            corsConfiguration.addAllowedMethod("*");//设置访问源请求方法(get、post等)

            return corsConfiguration;

        }

        @Bean

        public CorsFilter corsFilter() {

            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

            source.registerCorsConfiguration("/**", buildConfig());// 对接口配置跨域设置

            return new CorsFilter(source);

        }

    }

    ts和html部分

    @Component({

      selector: 'page-home',

      templateUrl: 'home.html'

    })

    export class HomePage {

      listData: Object;

      constructor(public navCtrl: NavController,public http:Http) {

      }

      printText(){

        this.http.request('http://127.0.0.1:8080/institutions/hello')

        .subscribe((res: Response) => {

          this.listData = res.json();

          console.log(this.listData);

        });

      }

    }

    相关文章

      网友评论

        本文标题:typescript与springboot跨域请求

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