相关文章推荐

問題現象

  • 瀏覽器報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 :選中 GET POST PUT DELETE HEAD

    • 允許Headers :設定為 *

    • 暴露Headers :設定為指定值或者不填。

  6. 單擊 確定

 
推荐文章