[Solved] from origin ‘http://localhost:4200’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource
This issue basically means that cors origin policy is having the issue or not configured properly. In this error it says, that localhost:4200 is not allowed in the backend.
In my case, spring boot application is my backend and the cors configuration is already configured like below,
Exception code:
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("*"));
// configuration.setAllowedOrigins(Arrays.asList("https://gururecharge.com","http://localhost:4200"));
configuration.setAllowedMethods(ImmutableList.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
// setAllowCredentials(true) is important, otherwise:
// The value of the 'Access-Control-Allow-Origin' header in the response must
// not be the wildcard '*' when the request's credentials mode is 'include'.
configuration.setAllowCredentials(true);
// setAllowedHeaders is important! Without it, OPTIONS preflight request
// will fail with 403 Invalid CORS request
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
But When I hit from my angular application which is running in localhost:4200 I was getting the below exception,
Access to XMLHttpRequest at 'https://localhost:8442/ngdeveloper/api/posts' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Working Code:
Then I modified the cors config the below way, then it worked.
Basically, this is the single line code change,
configuration.setAllowedOrigins(Arrays.asList("https://ngdeveloper.com","http://localhost:4200"));
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("*"));
configuration.setAllowedOrigins(Arrays.asList("https://gururecharge.com","http://localhost:4200"));
// setAllowCredentials(true) is important, otherwise:
// The value of the 'Access-Control-Allow-Origin' header in the response must
// not be the wildcard '*' when the request's credentials mode is 'include'.
configuration.setAllowCredentials(true);
// setAllowedHeaders is important! Without it, OPTIONS preflight request
// will fail with 403 Invalid CORS request
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
777sxgameapk? That’s the spot! Got me hooked up. Give it a look-see 777sxgameapk
Yo, Kkwinapk seems legit. Downloaded the app, was smooth sailing. Good selection of games. Try your luck! Get the app from: kkwinapk
P3bet22? Alright alright alright. Seen some ads, looks interesting. Variety is king, you know? Worth a look, I reckon. Right here: p3bet22
12playviet is not bad, maybe I try it on my free time this week. See for yourself: 12playviet
If you are looking for a seamless betting app experience this is it. Easy download and very intuitive design. Visit jeetbuzzappdownload.
Found this link through a friend and I’m glad I did. The platform is stable and the variety of options is impressive. Really enjoying my time here. stm88link
Very impressed with the customer support and the quick loading times. Definitely my new go-to spot at 16jililogin.