ich ein Problem mit Erhaltungs-Anforderung haben in Schräg 2. habe ich eine Methode Dokumente zu erhalten:Angular 2: Antwort für Preflight ungültig ist (Umleitung)
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());
}
Und es ist ein Fehler:
Response for preflight is invalid (redirect)
Lösung, es funktionierte für mich (in SpringBoot App):
@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;
}
Sie müssen den Server so konfigurieren, dass CORS zulässig ist. –
Ich habe es getan (hinzugefügt @ CrossOrigin (Ursprünge = "http: // localhost: 4200") in SpringBoot) und es funktioniert immer noch nicht. – Helosze
Ich weiß nichts über Ihren Server. Ich weiß, dass dies nicht mit Angular verwandt ist. Wahrscheinlich fehlen andere Konfigurationsoptionen. –