我正在制作一个java API REST和一个带有纯JS的前端应用程序(是的,没有框架),当我访问同一个域时,我的应用程序运行正常,但当我尝试访问另一个域时,我可以'吨。
我在java应用程序中的过滤器是这样的:
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");
});
我的JS请求是这样的:
static get(startDate,endDate){
return fetch(`http://localhost:4567/api/schedule/${startDate}/${endDate}`,{
mode : "cors",
})
.then(result => result.json())
.catch(error => erroe)
}
重要提示:如果我通过其他域访问,但直接在浏览器上访问,我会完全获得JSON 后端: java Spark frameWork
答案 0 :(得分:0)
我解决了我的问题,在我的应用程序JS中我调用了http://localhost:4567/api的请求,然后当我通过其他域访问应用程序时,应用程序调用self的请求,因此解决方案是通过应用程序更改localhost IP地址。