問題現象
-
瀏覽器報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 。
解決方案
按照以下步驟正確配置跨域規則。
-
登入 OSS管理主控台 。
-
單擊 Bucket 列表 ,然後單擊目標Bucket名稱。
-
在左側導覽列,選擇 。
-
在 跨域設置 頁面,單擊 創建規則 。
-
在 建立跨域規則 面板,按以下說明配置各項參數,其他參數保留預設配置。
-
來源 :設定為 * 。
-
允許Methods :選中 GET 、 POST 、 PUT 、 DELETE 、 HEAD 。
-
允許Headers :設定為 * 。
-
暴露Headers :設定為指定值或者不填。
-
-
單擊 確定 。