【问题标题】:Error: Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response错误:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权
【发布时间】: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

【问题讨论】:

    标签: angular laravel-passport


    【解决方案1】:

    错误信息很清楚,您的后端不允许使用'Authorization' 标头。 在您的后端 Laravel 应用程序中,您必须设置一个响应标头,其中包含:

    Access-Control-Allow-Headers : 'Content-Type', 'Authorization'
    

    在此处查看更多文档:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 2016-05-15
      • 2017-06-23
      • 2017-10-20
      • 2017-10-28
      • 1970-01-01
      • 2016-04-24
      • 2019-02-21
      相关资源
      最近更新 更多