2017-09-13 66 views
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; 
} 
+3

您需要配置服務器以允許CORS。 –

+0

我已經完成了它(在SpringBoot中添加@CrossOrigin(origins =「http:// localhost:4200」)),但它仍然無效。 – Helosze

+1

我不知道你的服務器。我知道這與Angular無關。可能還有其他配置選項丟失。 –

回答

0

這個問題是關於得到一個REST響應,但SER ver返回一個重定向。這不匹配。

我在這方面做了一些Angular/SpringBoot/Oauth2的測試,所以,這就是爲什麼我可以回答這個確切的情況。

相關問題