I have my front end ReactJS application, which calls AWS EC2 instance. This EC2 instance is running a Spring boot Jar file, which has simple GET request end point. But, when i run my reactjs application, which uses fetch api to get details from aws ec2 instance uri, it is throwing below error:
"Access to fetch at 'http://ec2-3-145-165-206.us-east-2.compute.amazonaws.com:8080/searchAll' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
But if i run the same aws url in Postman or browser, it just works fine. http://ec2-3-145-165-206.us-east-2.compute.amazonaws.com:8080/searchAll
ReactJS code:
async function fetchTableData() {
setLoading(true)
const URL = "http://ec2-3-145-165-206.us-east-2.compute.amazonaws.com:8080/searchAll"
const response = await fetch(URL)
const users = await response.json()
setData(users)
setLoading(false)
}
Could someone please tell me, what is wrong here not working from ReactJS app?