I am trying to call method of controller from reactJs and both are running on same machine but different port and it shows an error of Cors.
Access to XMLHttpRequest at 'http://localhost:8080/registration' from origin 'http://localhost:3000' 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.
Here is the code of React side:
const getHeaders = () => {
let authToken = localStorage.auth_token ? localStorage.auth_token : null;
let config = {
headers: {
Authorization: authToken,
'Accept': 'application/json',
},
};
return authToken ? config : {};
};
const axiosPost = async (data, url) => {
try {
debugger;
console.log(data);
console.log(url);
return await axios.post(`${BASE_URL}/${url}`, data, getHeaders());
} catch (error) {
checkError(error);
throw error.response.data;
}
};
This is controller from Java side:
@GetMapping(value = "/registration",produces = "application/json", consumes = "application/json")
public @ResponseBody ModelAndView Home(){
}