Spring - 前後端AJAX請求異常(POST一直顯示OPTIONS,跨域放開 CORS)

 

Ajax 跨域訪問post 請求,但是在服務器卻得到的總是options請求 (req.method==‘OPTIONS’)

 

1.跨域 

2.此post請求的 content-type不是one of the “application/x-www-form-urlencoded,multipart/form-data, or text/plain”

 

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


private CorsConfiguration buildConfig() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    List<String> list = new ArrayList<>();
    list.add("*");
    corsConfiguration.setAllowedOrigins(list);
/*
// 請求常用的三種配置,*代表允許所有,當時你也可以自定義屬性(比如header只能帶什麼,只能是post方式等等)
*/
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedMethod("*");
    return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", buildConfig());
    return new CorsFilter( source);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章