【问题标题】:Apigee + Angular No 'Access-Control-Allow-Origin' header is present on the requested resourceApigee + Angular 请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2018-06-09 23:20:15
【问题描述】:

早上;

也许这个问题在 100 次之前被问过,但我真的无法解决它。 我在 Apigee 中有响应 OPTIONS 请求的配置。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-AddCors">
    <DisplayName>AM-AddCors</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Access-Control-Allow-Origin">*</Header>
            <Header name="Access-Control-Allow-Headers">Content-Length, Content-Disposition, Origin, x-requested-with, Accept, Content-Type, Authorization</Header>
            <Header name="Access-Control-Max-Age">3628800</Header>
            <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

在客户端,我使用 Angular (HttpClient) 进行此调用

public downloadS2Report(url) {
    let headers = new HttpHeaders();
    let fullurl = environment.config.fundsApi.concat(url);
    headers = headers.set("Authorization", "Bearer *****");
    return this.http.get(fullurl, {headers: headers})
      .map((response:any) => {
        if (response.status == 200) {
          var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
          var blob = new Blob([(<any>response)._body], {type: contentType});
          return blob;
        }    
      });
  }

我可以在 chrome 的调试视图中看到结果,但我得到了著名的 Angular 错误“请求的资源上存在 Access-Control-Allow-Origin”标头”。我是否错过了 Apigee 配置中的参数?

【问题讨论】:

标签: angular cors apigee angular5


【解决方案1】:

您需要在所有响应中使用相关的CORS 标头进行响应,而不仅仅是OPTIONS 飞行前请求。因此,在您的GET 回复中,您还需要:

<Headers>
        <Header name="Access-Control-Allow-Origin">*</Header>
        <Header name="Access-Control-Allow-Headers">Content-Length, Content-Disposition, Origin, x-requested-with, Accept, Content-Type, Authorization</Header>
        <Header name="Access-Control-Max-Age">3628800</Header>
        <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
    </Headers>

【讨论】:

  • 谢谢你我不知道我在每次通话中都用这个参数回复
猜你喜欢
  • 2015-08-29
  • 2018-10-31
  • 2019-07-21
  • 2016-08-17
  • 2018-01-03
  • 2015-09-01
  • 2019-07-04
  • 1970-01-01
相关资源
最近更新 更多