fetch端:
let obj = {projectName:"xxx"}
fetch("http://localhost:8095/config/project/add",{
method:'post',
mode: 'cors',
credentials: 'omit',
headers:{'Content-type':'application/json;charset=utf8'},
body:JSON.stringify(obj)
}).then(response=>response.json())
.then(data=>console.log(data))
java/springboot服务器端:
@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);
}
}