I'm trying to download data from my rest api using react app. Code works with url from example on this site https://pusher.com/tutorials/consume-restful-api-react, but not working when I'v changed url to my local backend app.
My code:
fetch("http://localhost:8080/AppRest/appController/getData?ID=1")
.then(res => res.json())
.then((data) => {
alert(data)
this.setState({ icos: data })
})
.catch(console.log)
My errors in web browser console:
[Error] Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
[Error] Fetch API cannot load http://localhost:8080/AppRest/appController/getData?ID=1 due to access control checks.
[Error] Failed to load resource: Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. (getIcos, line 0)
[Log] TypeError: Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
I'v tested api and it prints json when I open page from url http://localhost:8080/AppRest/appController/getData?ID=1.
Below is my response header from backend app. This I can download when I connect to rest from web browser.
HTTP/1.1 200 OK
Content-Length: 65
Content-Type: application/json
X-Frame-Options: SAMEORIGIN
X-Powered-By: Servlet/4.0 JSP/2.3 (Payara Server 5.2020.7 #badassfish Java/Oracle Corporation/1.8)
Server: Payara Server 5.2020.7 #badassfish