Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource (POST request with object)
@Beanpublic CorsFilter corsFilter() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:4200")); corsConfiguration.setAllowedHeaders(Arrays.asList("Origin", "Access-Control, Allow-Origin", "Content-Type", "Accept", "Authorization", "Origin, Accept", "X-Requested-With", "Access-Control-Request-Method", "Access-Control-Request-Header" )); // this allows all headers corsConfiguration.setExposedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Authorization", "Access-Control-Request-Allow-Origin", "Access-Control-Allow-Credentials")); corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration); return new CorsFilter();}
I used this and it work fine for me
I ended up adding the following in my web.config
to make it work without any custom configuration on axios:
<system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0"/> <remove name="OPTIONSVerbHandler"/> <remove name="TRACEVerbHandler"/> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> </handlers> </system.webServer>
I was getting the same error. I allowed cors from both application side and server side. But after searching, i found out there might be region issue so i added proxy url with the original url and it worked for me. Here is a code:
var myHeaders = new Headers();myHeaders.append("Authorization", "Bearer "+ token);var requestOptions = { method: 'GET', headers: myHeaders, Vary: 'Origin',};const proxyurl = "https://cors-anywhere.herokuapp.com/";const url = "your url";fetch(proxyurl + url, requestOptions) .then(response => response.json()) .then(result => console.log(result)) .catch(error => console.log('error', error));