我无法访问另一个域的 api
I can't access my api of the another domain
我正在制作一个 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
我解决了我的问题,在我的应用程序 JS 中我调用了 http://localhost:4567/api 的请求,然后当我被其他域访问应用程序时,应用程序调用自己的请求,所以解决方案是更改 localhost通过应用程序 ip 地址。
我正在制作一个 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
我解决了我的问题,在我的应用程序 JS 中我调用了 http://localhost:4567/api 的请求,然后当我被其他域访问应用程序时,应用程序调用自己的请求,所以解决方案是更改 localhost通过应用程序 ip 地址。