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無關。可能還有其他配置選項丟失。 –