【发布时间】:2025-12-25 01:35:16
【问题描述】:
我有一个正在工作的创建表单,但是,由于 CORS,我的图像似乎没有上传到 S3
我得到以下信息
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://henrywatches-dev.s3.amazonaws.com/. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
我有以下fetch 请求
if (isValid) {
fetch("/api/collections/create", {
method: "POST",
mode: "cors",
body: JSON.stringify(this.state),
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
})
.then(res => res.json())
.then(() => uploadFile(this.state.images, this.s3Config))
.then(
() => this.setState({ successMsg: true }),
() => this.setState(initalState)
)
.catch(error => {
console.log(error);
});
}
就像我提到的表单正在工作并且确实创建了一条记录,只是 S3 .then(() => uploadFile(this.state.images, this.s3Config)) 被阻止了。我已经使用 express 设置了 cors,但是由于 S3 发生在前端,cors 再次弹出,因为它没有发生在服务器端。
我已经尝试过mode: 'cors',但这在修复错误方面没有任何作用。
【问题讨论】:
-
响应的 HTTP 状态码是什么?您可以使用浏览器开发工具中的网络窗格进行检查。是 4xx 还是 5xx 错误而不是 200 OK 成功响应?
标签: reactjs amazon-web-services amazon-s3 cors