文档

通过CDN跨域访问资源时发生报错

更新时间:
一键部署

问题描述

在以下场景中使用阿里云CDN产品后,访问资源时提示以下错误:

The value of the 'Access-Control-Allow-Origin' header login.html:1 in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 场景一:CDN配置的加速源站中,请求携带“credentials”参数发生报错。
  • 场景二:CDN加速OSS资源,在客户端请求加速资源时发生报错。

问题原因

在以上两个场景中发生报错的原因如下:

  • 场景一:CDN配置的加速源站中,没有在请求头和源站的响应头中配置“credentials”参数。
  • 场景二:CDN加速OSS时,OSS不支持设置HTTP响应头“Access-Control-Allow-Credentials”。

解决方案

根据CDN的具体使用场景,参见以下操作进行处理:

  • 场景一:CDN配置的加速源站中,需要设置源站返回头中有参数“Access-Control-Allow-Credentials:true”。如何配置响应头参数,请参见配置自定义HTTP响应头
  • 场景二:CDN加速OSS时,在请求时不要设置“Access-Control-Allow-Credentials”参数,如果无法修改请咨询阿里云技术支持,以申请配置域名的HTTP头“Access-Control-Allow-Credentials:true”。

更多信息

Access-Control-Allow-Credentials参数说明如下:

  • “Access-Control-Allow-Credentials”响应头表示是否可以将对请求的响应暴露给页面。返回“true”则可以,其他值均不可以。
  • credentials”可以是Cookies、Authorization headers或TLS client certificates。当作为对预检请求的响应的一部分时,表示是否为真正的请求可以使用“credentials”。注意简单的“GET”请求没有预检,所以如果对资源的请求带了“credentials”,返回这个资源时,响应就会被浏览器忽视,不会返回到Web内容。
  • “Access-Control-Allow-Credentials”响应头在工作中与“XMLHttpRequest.withCredentials”或Fetch API的“Request()”构造器中的“credentials”选项结合使用。必须在前后端配置“credentials”,即“Access-Control-Allow-Credentials”header、XHR和Fetch Request中都要配置,才能使携带“credentials”的CORS请求成功。

相关文档

适用于

  • CDN

 

  • 本页导读
文档反馈