我已经阅读了很多关于此的主题,但它仍然不起作用,我正在失去理智。
我有一个带有 Spring Api 的 Angular6 应用程序,当我调用它时,我一直遇到这个错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
然而我觉得我做了一切:
这是我对 Angular App 中的 api 的调用:
getGamesFromServer() {
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin':'*'
})
};
this.httpClient
.get<any[]>('http://localhost:8080/api/rest/v1/game/progress', httpOptions)
.subscribe(
(response) => {
console.log(response);
this.games = response;
console.log(this.games);
this.emitGameSubject();
},
(error) => {
console.log('Erreur ! : ' + error);
}
);
}
这是调用的java方法:
@RequestMapping(method = GET, value = "/progress")
@ResponseBody
public Response findAllAndProgress() {
return Response.status(Response.Status.OK)
.entity(gameService.findAllAndProgress())
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.build();
}
我还启动了 chrome 并激活了 --disable-web-security 选项,我还下载了一个 chrome 扩展以允许 CORS 请求,但它没有用
RISEBY
慕斯王
相关分类