【发布时间】:2018-08-13 00:19:44
【问题描述】:
Failed to load URL: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
是否可以在不更改服务器端代码(Java Spring 控制器)的情况下从 Angular2 修复此问题?
我的代码
const headers = new Headers({'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*'
});
return this.http.get(url, {headers: headers})
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
【问题讨论】:
-
想象一下你可以绕过客户端的 CORS...
-
不,这不能从客户端绕过,图像您可以,攻击者在您登录时使用它来读取您的银行网站。
标签: javascript java angular cors