我正在Angular 2和Laravel 5.4中构建我的应用程序。 Angular2用于客户端,laravel 5.4用于服务器端。我在laravel中创建了API并从Angular2请求了这些API。
在Laravel中,我配置了Oauth 2护照服务,该服务正在验证所有API。在每个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
错误消息很明显,后端不允许使用qazxsw poi标头。在后端Laravel应用程序中,您必须设置一个响应头,其中包含:
'Authorization'
请参阅此处的进一步文档:Access-Control-Allow-Headers : 'Content-Type', 'Authorization'