【发布时间】:2017-12-08 19:25:56
【问题描述】:
我有一个 REST-API 可以联系,它可以大摇大摆地工作。 现在我尝试用我的 Angular 4 应用程序调用它。 我得到了错误:
XMLHttpRequest cannot load
"baseURL"/company/. 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
当我查看调用时,我发现它没有传递任何标头(也不是令牌),但实际上我在 Angular 4 代码中将它们添加到我的请求中:
getCompanies(): Observable<Company[]> {
let tokenobject =JSON.parse(sessionStorage.getItem('current_user'));
let token = tokenobject.token;
let headers = new Headers({ 'Access-Control-Allow-Origin' : 'http://localhost:4200','Authorization': token });
let options = new RequestOptions({ headers: headers });
return this.http.get(this.url + this.path, options)
.map(this.extractData)
.catch(this.handleError);
}
有什么我忘记或做错了吗?
【问题讨论】: