所请求的资源上没有“ Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://localhost:4200”。
request = request.clone({
setHeaders: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'http://localhost:4200',
Authorization: 'Bearer ' + this.authToken
},
});
在后端服务器上,我们将允许交叉原点设置如下
response.setHeader("Access-Control-Allow-Origin", "*");
System.out.println("Request header: " + request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Authorization, X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin");
,但是我们仍然没有获得访问控制允许源出现在请求的资源上。
答案 0 :(得分:0)
如果您使用的是Angular6。它支持proxy-config.json,它将代理您的后端API。它仅用于开发。有关更多详细信息,请参阅他的链接https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md