0
我有在角2 GET请求一个问题,我有一个方法来获取文件:角2:预检响应无效(重定向)
getDocuments() {
let username: string = 'username';
let password: string = 'password';
let headers: Headers = new Headers();
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "multipart/form-data");
return this._http.get('http://localhost:8080/api/documents', {headers: headers}).map(res => res.json());
}
而且有一个错误:
Response for preflight is invalid (redirect)
解决方案,它为我工作(在SpringBoot应用添加):
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
org.springframework.web.cors.CorsConfiguration config = new org.springframework.web.cors.CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:4200");
config.setAllowedMethods(Arrays.asList("POST", "OPTIONS", "GET", "DELETE", "PUT"));
config.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
您需要配置服务器以允许CORS。 –
我已经完成了它(在SpringBoot中添加@CrossOrigin(origins =“http:// localhost:4200”)),但它仍然无效。 – Helosze
我不知道你的服务器。我知道这与Angular无关。可能还有其他配置选项丢失。 –