文档

OSS跨域资源共享(CORS)错误排除

更新时间:

问题现象

  • 浏览器报403错误,具体如下:

    OPTIONS http://bucket.oss-cn-beijing.aliyuncs.com/
    XMLHttpRequest cannot load http://bucket.oss-cn-beijing.aliyuncs.com/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '{yourwebsite}' is therefore not allowed access. The response had HTTP status code 403.
  • OSS报CORS请求不允许的错误,具体如下:

    <Code>AccessForbidden</Code>
    <Message>CORSResponse: This CORS request is not allowed. This is usually because the evaluation of Origin, request method / Access-Control-Request-Method or Access-Control-Request-Headers are not whitelisted by the resource's CORS spec.</Message>

可能原因

跨域规则配置错误。

说明
  • CORS报错一般是站点类应用导致,浏览器中可以查看请求详情。例如在Chrome浏览器按F12打开开发者工具 ,在Network页签中查看相应元素。

  • OSS返回的错误可以通过抓包获取。如果使用Wireshark抓取和分析数据,则筛选器可以指定为host bucket-name.oss-cn-beijing.aliyuncs.com

解决方案

按照以下步骤正确配置跨域规则。

  1. 登录OSS管理控制台

  2. 单击Bucket 列表,然后单击目标Bucket名称。

  3. 在左侧导航栏,选择数据安全 > 跨域设置

  4. 跨域设置页面,单击创建规则

  5. 创建跨域规则面板,按以下说明配置各项参数,其他参数保留默认配置。

    • 来源:设置为*

    • 允许Methods:选中GETPOSTPUTDELETEHEAD

    • 允许Headers:设置为*

    • 暴露Headers:设置为指定值或者不填。

  6. 单击确定

  • 本页导读 (1)
文档反馈