In my spring server I added this to enable cors:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/*").allowedOrigins("http://localhost:3000");
}
};
}
In my react.js client when I POST I get no error, but when I try to GET, I get error saying :
Access to XMLHttpRequest at 'http://localhost:8080/user/aweq' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Code :
let url=constant.serverURL+"user/"+this.state.username;
axios.get(url).then((res)=>console.log(res));
I even tried this :
let url=constant.serverURL+"user/"+this.state.username;
axios.get(url,
{headers:
{
"Content-Type": "application/json"
}
}
).then((res)=>console.log(res));