【发布时间】:2017-10-15 12:29:44
【问题描述】:
我正在使用 Angular 2 和 Laravel 5.4 构建我的应用程序。 Angular2 用于客户端,laravel 5.4 用于服务器端。我在 laravel 中创建了 API,并从 Angular2 请求这些 API。
在 Laravel 中,我配置了验证所有 API 的 Oauth 2 护照服务。在每个 API 调用中,我都会发送以下标题。
“内容类型”、“授权”
我如何使用标头调用 API。
private headers = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('access_token') });
let body = JSON.stringify({ user_id, company_id });
console.log(this.headers);
this.http.post(this.BuyerCostSetting, body, {headers:this.headers} )
.subscribe(
response => {
console.log('here');
console.log(response);
},
error => {
console.log(error.text());
}
);
当我从 Angular2 访问这个 API 时,它显示以下错误:
Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response
【问题讨论】: