I'm making a java API REST and a front-end application with pure JS(yes, no framework), when I access by same domain, my application works perfectly, but when I try access by another domain I can't.
My filter in java application is like this:
after((req, resp) -> {
resp.header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
resp.header("Access-Control-Allow-Methods", "GET,PUT,DELETE,POST,OPTIONS");
resp.header("Content-Encondig", "gzip");
resp.header("Access-Control-Allow-Origin", "*");
resp.type("application/json");
});
And my JS request is like this:
static get(startDate,endDate){
return fetch(`http://localhost:4567/api/schedule/${startDate}/${endDate}`,{
mode : "cors",
})
.then(result => result.json())
.catch(error => erroe)
}
Important: if I access by another domain, but directly on browser, I get the JSON perfectly Backend : java Spark frameWork
I solved my problem, in my application JS I called the request for http://localhost:4567/api, then when I was access the application by other domain, the application call the request for self, so the solution was change localhost by application ip address.