文档

发送跨域请求时报错Access-Control-Allow-Origin不能为*

更新时间:

问题描述

在OSS中将Access-Control-Allow-Origin的值设置为通配符"*",并在前端代码中将Access-Control-Allow-Credentials设置为true,然后在浏览器跨域访问OSS文件时,出现以下报错:

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

问题原因

浏览器的安全策略要求Access-Control-Allow-Credentials为True时,Access-Control-Allow-Origin的值不能为通配符"*",以防止任意域访问资源并获取Credentials信息(包括Cookies、Authorization Headers等敏感数据)。

解决方案

  • 如果您需要在请求头中保留Credentials信息,将Access-Control-Allow-Origin的值从通配符*修改为具体的域名(例如https://example.com)。具体操作,请参见跨域设置

  • 如果您不需要在请求头中保留Credentials信息,您可以在前端代码中将Access-Control-Allow-Credentials设置为false。示例代码如下。

    xhr.withCredentials = false
  • 本页导读 (1)
文档反馈